Run Code from User Form when Check box Clicked

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,411
I have the following code in my Private Sub UserForm_initialize() section of my user form.

It will programmatically create many check boxes. this part actually works as planned.

VBA Code:
r = 1
For c = 2 To 14
    Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & c)
    chkBox.Caption = Cells(10, c).Value
    chkBox.Left = 500
    chkBox.Top = 50 + ((r - 1) * 20)
    
    If Columns(c).ColumnWidth = 0 Then chkBox.Value = True 'delaut check boxes to true if data in row 11
    r = r + 1
Next c

the issue is, I do not know how to trigger a click event

The below does not seem to work:
VBA Code:
Private Sub CheckBox_3_Click()
MsgBox "Box 3 clicked"
End Sub

any ideas?

Thanks,
Ross
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
VBA Code:
If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then

1 is checked
0 is unchecked
 
Upvote 0
Logit,
not sure we are on the same page.

I think your code returns the value of the control
I would like an event to run as soon as the user clicks or unclicks each individual control.
 
Upvote 0
Ross,

Please try:

VBA Code:
Private Sub CheckBox_3_Change()
MsgBox "Box 3 clicked"
End Sub
 
Upvote 0
if I put a check box on my form manually it works just fine.

I think the problem lies in the fact that the check boxes themselves were created with VBA code.
 
Upvote 0
VBA Code:
If Me.Shapes("Check Box 1").ControlFormat.Value = 1 Then
    'Your macro code here or a call to the macro. Ex: Your Macro
    'is named ThisIsMyMacro. So you would put ...
   
    ThisIsMyMacro
End if
 
Upvote 0
Logit,

Again, I think you code is getting the value. I'm not looking for that.
I am Looking for the method to trigger an action when the check box is clicked by the user.
 
Upvote 0
Let's try it this way .....

VBA Code:
Sub CommandButton2_Click()
  If Me.CheckBox6.Value = True Then
    Range("A7").Select
    ActiveCell.Value = "BOX 6"
    Range("A1").Select
    MsgBox "Done"
  End If
End Sub

Download workbook : Amazon Drive
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top