我需要一个VBA代码来隐藏复选框,直到检查另一个复选框? [英] I need a VBA code to hide a check box until another checkbox is checked?
问题描述
我需要隐藏复选框Two_one_WP,直到复选框Two_zero被点击任何想法???
我尝试了什么:
I need the checkbox "Two_one_WP" to be hidden until checkbox "Two_zero" is clicked any ideas???
What I have tried:
Private Sub Two_zero_Click()
If Two_zero = True Then
[2].EntireRow.Hidden = False
Else: [2].EntireRow.Hidden = True
End If
End Sub
Private Sub Two_one_WP_Click()
If Two_one_WP = True Then
[3:7].EntireRow.Hidden = False
Else: [3:7].EntireRow.Hidden = True
End If
End Sub
推荐答案
为什么要在电子表格上隐藏行?
只需切换Checkbox的可见
属性,例如将此代码放入ThisWorkbook VBA模块适用于我
Why are you trying to hide rows on the spreadsheet?
Just toggle theVisible
property of the Checkbox, e.g. putting this code into the ThisWorkbook VBA module works for me
Option Explicit
Private Sub Two_zero_Click()
If Two_zero.Value Then
two_one_WP.Visible = True
Else
two_one_WP.Visible = False
End If
End Sub
值得注意的是,我使用的Checkbox对象是ActiveX控件而不是Form控件。如果你使用表单控件,那么你需要这样的东西
It's worth noting that the Checkbox objects I used were the ActiveX controls not the Form Controls. If you use the Form Controls then you need something like this instead
Sub Two_zero_Click()
If ActiveSheet.Shapes("Two_zero").ControlFormat.Value = 1 Then
Sheets("Sheet1").Shapes("two_one_WP").Visible = True
Else
Sheets("Sheet1").Shapes("two_one_WP").Visible = False
End If
End Sub
[OP评论后编辑]
您声称上面的代码中断您的代码以隐藏行。它不能。
如果您希望所有子类别复选框也消失,那么您可以扩展我给您的代码以包含所有这些。没有什么可以阻止你隐藏行,如果这是你想要做的。
然而,如果你想尝试隐藏复选框隐藏它们所在的行然后您需要将它们设置为移动并使用单元格调整大小。 此StackOverflow帖子 [ ^ ]将告诉你如何做到这一点(这取决于哪个版本的Excel你正在使用)。
一旦你在复选框上设置了该属性,那么当你隐藏一行时,该行的on复选框也将被隐藏。
You claim that the code above "disrupts" your code to hide rows. It can't.
If you want all the sub-category checkboxes to also disappear then you could just expand the code I've given you to include all of them. There is nothing to stop you hiding the rows as well if that is what you want to do.
However, if you are trying to hide the checkboxes by hiding the rows that they are on then you need to set them to "Move and Size with Cells". The accepted answer on this StackOverflow post[^] will show you how to do that (it depends on which version of Excel you are using).
Once you have set that property on the checkbox(es) then when you hide a row the checkbox "on" that row will also be hidden.
这篇关于我需要一个VBA代码来隐藏复选框,直到检查另一个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!