Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 3 of 3

Thread: Change multiple checkbox properties with For, Next

  1. #1
    Guest

    Default

    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


  2. #2
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  3. #3
    Guest

    Default

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

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •