Change multiple checkbox properties with For, Next

G

Guest

Guest
Problem: I need to make a loop to enable/disable 20 checkboxes on a Userform when Checkbox1.value changes.

The code changes remaining checkbox values to TRUE when Checkbox1 is "checked." Can't seem to use the same technique for "disable" - Ctrl.disable didn't work. Don't want to have 20 lines of code to disable the checkboxes. Is there a way to condense the last code lines in a loop?

Private Sub checkbox1_change()
If CheckBox1.Value = True Then
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
ctrl.Value = True
Next ctrl
End If
CheckBox2.Enabled = Not CheckBox1.Value
CheckBox3.Enabled = Not CheckBox1.Value
CheckBox4.Enabled = Not CheckBox1.Value
.
.
.
.
etc.

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
This should give you an idea. Hope it helps!

Code:
Private Sub CheckBox1_Change()
    Dim ctl As Control
    
    If CheckBox1 Then
        For Each ctl In Me.Controls
            If ctl.Name <> "CheckBox1" Then
                Controls(ctl.Name).Enabled = True
                ctl = True
            End If
        Next
    Else
        For Each ctl In Me.Controls
            If ctl.Name <> "CheckBox1" Then
                Controls(ctl.Name).Enabled = False
            End If
        Next
    End If

End Sub

-rh
 
Upvote 0
Thanks RH -
That was the hint I needed. "Controls(ctrl.Name).Enabled" did the trick.
Thanks!
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top