UserForm CheckBox in Excel VBA – Explained with Examples

VBA ActiveX CheckBox Control on the UserForm

  1. Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
  2. Go To Insert Menu, Click UsereForm. Please find the screenshot for the same.
  3. Excel VBA UserForm CheckBox

  4. Drag a check box on the Userform from the Toolbox. Please find the screenshot for the same.
  5. AxtiveX Checkbox Control1

  6. Now, you can see the following code.
  7. Private Sub CheckBox1_Click()
    
    End Sub
    

    Note : In the above code ‘ CheckBox1 ’ is the Check box name .

  8. Please add the following statements to the procedure.
  9. 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
    
  10. Run the above macro by pressing F5.
  11. Check and uncheck the check box twice to get the two different outputs.

Add dynamic CheckBox Control on the UserForm using VBA

Please find the keep up steps and case code, it will show you how to add dynamic checkbox master on the userform.

  1. Add command button on the userform from the toolbox.
  2. Right click on the command button, click properties
  3. Change the command button caption to ‘Create_Checkbox’
  4. Double click on the command button
  5. Now, it shows following code.
  6. Private Sub CommandButton1_Click()
     
    End Sub
    
  7. Call the below procedure named ‘Add_Dynamic_Checkbox’ and find the below procedure to run.
  8. 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  
    
  9. Now, click F5 to run the macro, click ‘Create_Checkbox’ button to see the result.
  10. You can see the created dynamic check box in the following screen shot.
  11. output :
    Dynamic Checkbox

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 downstairs example we are using value property of the determine box to select or UnSelect a crack 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 case we are using respect 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 condition on the Worksheet or UserForm in Excel.
Read More …

source : https://coinselected
Category : coin 4u

Leave a Reply

Your email address will not be published.