Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through CheckBoxes on UserForm?

Tags:

excel

vba

By selecting an OptionButton I want all CheckBoxes on the active form to deselect AND UNCHECK.

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=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

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.

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.

like image 449
tnuis Avatar asked Feb 13 '23 17:02

tnuis


1 Answers

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

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
like image 98
David Zemens Avatar answered Feb 15 '23 08:02

David Zemens