CheckBox is one of the UserForm control. You can select and drag CheckBox on the UserForm. CheckBox Control is used to specify or indicate binary choice. That is either Turn on or off a value. When we use more checkboxs, you can select more than one CheckBox at a time on the UserForm. You can select multiple check boxes in a group box. Please find more details about CheckBox Control in the following chapter.
VBA ActiveX CheckBox Control on the UserForm
- Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
- Go To Insert Menu, Click UsereForm. Please find the screenshot for the same.
- Drag a check box on the Userform from the Toolbox. Please find the screenshot for the same.
- Now, you can see the following code.
- Please add the following statements to the procedure.
- Run the above macro by pressing F5.
- Check and uncheck the check box twice to get the two different outputs.
Private Sub CheckBox1_Click() End Sub
Note: In the above code ‘CheckBox1’ is the Check box name.
Private Sub CheckBox1_Click() If UserForm2.CheckBox1.Value = True Then MsgBox "Checkbox has Checked", , "Checkbox" Else MsgBox "Checkbox has UnChecked", , "Checkbox" End If End Sub
Add dynamic CheckBox Control on the UserForm using VBA
Please find the following steps and example code, it will show you how to add dynamic checkbox control on the userform.
- Add command button on the userform from the toolbox.
- Right click on the command button, click properties
- Change the command button caption to ‘Create_Checkbox’
- Double click on the command button
- Now, it shows following code.
- Call the below procedure named ‘Add_Dynamic_Checkbox’ and find the below procedure to run.
- Now, click F5 to run the macro, click ‘Create_Checkbox’ button to see the result.
- You can see the created dynamic check box in the following screen shot.
Private Sub CommandButton1_Click() End Sub
Private Sub CommandButton1_Click() Call Add_Dynamic_Checkbox End Sub
Procedure to call in the Command Button:
Sub Add_Dynamic_Checkbox() 'Add Dynamic Checkbox and assign it to object 'Cbx' Set Cbx = UserForm2.Controls.Add("Forms.CheckBox.1") 'Assign Checkbox Name Cbx.Caption = "Checkbox2" 'Checkbox Position Cbx.Left = 10 Cbx.Top = 10 End Sub
Select or UnSelect a CheckBox using VBA?
Please find the below code to know how to select or UnSelect a check box using VBA. In the below example we are using value property of the check box to select or UnSelect a check box.
‘ To select check box CheckBox1.Value=True ‘ To unselect check box CheckBox1.Value=False
Check if a check box is selected or not using VBA
Please find the below code to know how to check if a check box is selected or not using VBA. In the below example we are using value property of the check box.
Sub ChkBox_Ex1() If CheckBox1.Value = True Then MsgBox “CheckBox has selected” Else MsgBox “CheckBox has not selected” End If End Sub
More details about Check box control
Here is the link more about how to add check box control on the Worksheet or UserForm in Excel.
Read More …
Here is the one more link to more about how to add check box control on the Worksheet or UserForm using VBA in Excel.
Read More …
Here is the one more link to more about how to remove check box control on the Worksheet or UserForm in Excel.
Read More …