vba 如何遍历用户窗体上的复选框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24221346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop through CheckBoxes on UserForm?
提问by tnuis
By selecting an OptionButton I want all CheckBoxes on the active form to deselect AND UNCHECK.
通过选择一个 OptionButton,我希望活动表单上的所有复选框都取消选择和取消选中。
I can get it to deselect using the For Each Loop but this does not work for unchecking the boxes. I get the error:
我可以让它使用 For Each 循环取消选择,但这不适用于取消选中这些框。我收到错误:
chB.Value=Object variable or With block variable not set
chB.Value=对象变量或未设置块变量
Private Sub optB_9201_Click()
Dim ctrl As Control
Dim chB As CheckBox
If Me.optB_9201.Value = True _
And Me.optB_9251.Value = False _
And Me.optB_9301.Value = False Then
Me.img9301_main.Visible = True
Me.frM9301_View.Caption = "Du har valgt CLX-9201NA med f?lgende konfigurasjon:"
Me.frm9301_Equipment.Enabled = True
For Each ctrl In Me.frm9301_Equipment.Controls
ctrl.Enabled = False
chB.Value = False
Next ctrl
Me.frM9301_Stand.Enabled = True
For Each ctrl In Me.frM9301_Stand.Controls
ctrl.Enabled = True
Next ctrl
End If
End Sub
How to fix this?
如何解决这个问题?
Alternatively:
或者:
Is is possible to have a change event on the UserForm that states that if a CheckBox is Enabled= False Then Value=False.
是否可以在用户窗体上有一个更改事件,指出如果复选框已启用 = False,则值 = False。
This way I would not have to put in the For Each Loop on every OptionButton. I tried the UserForm_Change Sub and UserForm_Click but nothing seems to work.
这样我就不必在每个 OptionButton 上放入 For Each 循环。我尝试了 UserForm_Change Sub 和 UserForm_Click 但似乎没有任何效果。
回答by David Zemens
You never Assign anything to chb
(and I'm not sure that you need to use that variable at all). You could do:
您从不分配任何内容chb
(我不确定您是否需要使用该变量)。你可以这样做:
For Each ctrl In Me.frm9301_Equipment.Controls
ctrl.Enabled = False
ctrl.Value = False
Next ctrl
This will only work if allcontrols are CheckBoxes. If that is not the case, then just add some if/then logic:
这仅在所有控件都是 CheckBox时才有效。如果不是这种情况,那么只需添加一些 if/then 逻辑:
For Each ctrl In Me.frm9301_Equipment.Controls
If TypeName(ctrl) = "CheckBox" Then
ctrl.Enabled = False
ctrl.Value = False
End If
Next ctrl