如何遍历UserForm上的CheckBoxes? [英] How to loop through CheckBoxes on UserForm?
问题描述
通过选择一个OptionButton,我希望活动表单上的所有复选框都取消选中并取消选中.
By selecting an OptionButton I want all CheckBoxes on the active form to deselect AND UNCHECK.
我可以使用For Each循环取消选择它,但是这对于取消选中这些框不起作用.我收到错误消息:
I can get it to deselect using the For Each Loop but this does not work for unchecking the boxes. I get the error:
chB.Value =对象变量或未设置块变量
chB.Value=Object variable or With block variable not set
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
该如何解决?
或者:
是否有可能在用户窗体上发生更改事件,该事件指出如果CheckBox启用= False,则Value = False.
Is is possible to have a change event on the UserForm that states that if a CheckBox is Enabled= False Then Value=False.
这样,我不必在每个OptionButton上都放入For Each循环.我尝试了UserForm_Change Sub和UserForm_Click,但似乎无济于事.
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.
推荐答案
您永远不会为chb
分配任何内容(而且我不确定您是否完全需要使用该变量).您可以这样做:
You never Assign anything to chb
(and I'm not sure that you need to use that variable at all). You could do:
For Each ctrl In Me.frm9301_Equipment.Controls
ctrl.Enabled = False
ctrl.Value = False
Next ctrl
这仅在所有控件为CheckBoxes时有效.如果不是这种情况,则只需添加一些if/then逻辑:
This will only work if all controls are CheckBoxes. If that is not the case, then just add some if/then logic:
For Each ctrl In Me.frm9301_Equipment.Controls
If TypeName(ctrl) = "CheckBox" Then
ctrl.Enabled = False
ctrl.Value = False
End If
Next ctrl
这篇关于如何遍历UserForm上的CheckBoxes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!