hindersaliva
asked on
Excel VBA - UserForm control click
I have 14 CheckBoxes on a UserForm. 'chk1AM', chk1PM .... to ckh7AM, chk7PM.
I want to avoid having 14 _Click events. Would it be possible to have an 'event' that will be triggered if any of these are clicked? (So then I determine which one was clicked as per the other question I asked here earlier)
Thanks
I want to avoid having 14 _Click events. Would it be possible to have an 'event' that will be triggered if any of these are clicked? (So then I determine which one was clicked as per the other question I asked here earlier)
Thanks
That's not possible with VBA, but why not put all those values in one control like a combobox and have them pick from there? Can you attach your workbook?
Other than that I think the only thing you can do is to create a Sub that does what you want, and add one line of code to each checkbox that calls that sub passing the name of the control.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Could you send a dummy?
then try
Dim CheckBoxesColl As Collection
Private Sub UserForm_Initialize()
Dim CheckBoxHandler As ClsCheckBoxEvent
Set CheckBoxesColl = New Collection
For Each ctitem In Me.Controls
If TypeName(ctitem) = "CheckBox" Then
Set CheckBoxHandler = New ClsCheckBoxEvent
Set CheckBoxHandler.CheckGroup = ctitem
CheckBoxesColl.Add CheckBoxHandler
End If
Next
End Sub
ASKER
Rgonzo, now I don't get the error but the Checkbox click doesn't produce the message box.
a dummy would be helpful
ASKER
See attached. Ignore the sheet, go to UserForm.
EE-troubleshoot-controls-click-clas.xlsm
EE-troubleshoot-controls-click-clas.xlsm
see remark
'Dim CheckBoxesColl As Collection 'should be and is already defined as global
Dim CheckBoxHandler As clsCheckboxEvent
Set CheckBoxesColl = New Collection
For Each ctitem In Me.Controls
If TypeName(ctitem) = "CheckBox" Then
Set CheckBoxHandler = New clsCheckboxEvent
Set CheckBoxHandler.CheckGroup = ctitem
CheckBoxesColl.Add CheckBoxHandler
End If
Next
your captions are all the same then use name
Option Explicit
Public WithEvents CheckGroup As MSForms.CheckBox
Private Sub CheckGroup_Click()
MsgBox CheckGroup.Name
End Sub
ASKER
Thanks Rgonzo. That worked beautifully!