Home » excel » excel – VBA – Multiple Checkboxes Assigned to Same Macro, How to Determine which Checkbox Fired Event?

excel – VBA – Multiple Checkboxes Assigned to Same Macro, How to Determine which Checkbox Fired Event?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have about 20 checkboxes on an excel sheet. I want to detect when a checkbox is clicked and which checkbox is clicked.

I know I could make a method for each checkbox but this is definitely not the best way. So I assigned each checkbox to the same macro:

Public Sub fieldCheck_Click()
    MsgBox "checkbox clicked"
End Sub

How can I determine which checkbox fired the event?

How to&Answers:

You can look at Application.Caller to get the name of the clicked checkbox, or you can pass one or more parameters in the OnAction setting: eg set the “macroname” to

'fieldCheck_Click "Item1"' (including the single- and double-quotes) 

or

'fieldCheck_Click "Item1", 55'