Creating A Userform with Checkboxes in VBA
Updated on February 2, 2013
- Open the Visual Basic Editor
- Create a UserForm (Right Click on Micrsoft Excel Object, select insert>userform)
- Show the Control Toolbox (View>Toolbox)
- Click the Checkbox on the toolbox(Drag to appropriate size and location on Form)
- Click the Commandbox on the (Drag to appropriate size and location on form)
- Create conditional code based on checkbox value. If checked value=True, Unchecker value=false
- Go back to the form double click on command button and call the conditional macro.
- Back in the main module create a new macro to show the form.
Code for Userform
Private Sub CommandButton1_Click()
Call onexecute
End Sub
Sample Conditional Code for Checkbox
Sub ShowUserForm()
UserForm1.Show
End Sub
Sub onexecute()
If UserForm1.CheckBox1.Value = True Then
Call macro1
End If
If UserForm1.CheckBox2.Value = True Then
Call macro2
End If
If UserForm1.CheckBox3.Value = True Then
Call macro3
End If
End Sub
Sub macro1()
MsgBox ("Checkbox 1 is checked.")
End Sub
Sub macro2()
MsgBox ("Checkbox 2 is checked.")
End Sub
Sub macro3()
MsgBox ("Checkbox 3 is checked.")
End Sub