Clear All Check Boxes In Excel VBA Userform

This example uses a for each loop so that each check box name does not have to be explicitly written out. This is useful when you are adding more control and limit the amount of code you are writing.

Better Method

Dim ctrl As Control

Sub ClearAllButton_Click()
    For Each ctrl In exportFilesUF.Controls
        If TypeName(ctrl) = "CheckBox" Then
            ctrl.Value = True
        End If
    Next
End Sub


Private Sub SelectAllButton_Click()
    For Each ctrl In exportFilesUF.Controls
        If TypeName(ctrl) = "CheckBox" Then
            ctrl.Value = False
        End If
    Next
End Sub

Worse Method

Sub ClearAllButton_Click()
    CheckBox4.Value = True
    CheckBox9.Value = True
    CheckBox10.Value = True
    CheckBox11.Value = True
    CheckBox12.Value = True
    CheckBox7.Value = True
    CheckBox8.Value = True
    CheckBox6.Value = True
    CheckBoxCNC_all.Value = True
    CheckBox_CNCNodes.Value = True
    CheckBox_CNCBeams.Value = True
    CheckBox_CNCPanels.Value = True
End Sub

Private Sub SelectAllButton_Click()
    CheckBox4.Value = False
    CheckBox9.Value = False
    CheckBox10.Value = False
    CheckBox11.Value = False
    CheckBox12.Value = False
    CheckBox7.Value = False
    CheckBox8.Value = False
    CheckBox6.Value = False
    CheckBoxCNC_all.Value = False
    CheckBox_CNCNodes.Value = False
    CheckBox_CNCBeams.Value = False
    CheckBox_CNCPanels.Value = False
End Sub


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s