Reputation: 3
I am trying to write some code for a VBA userform that has about 100 checkboxes. I was wondering if there is a way that I could have one piece of code that applies to any checkbox or if I have to write 100 seperate functions for checkbox1_click, checkbox2_click, checkbox3_click, etc.
Thanks for any help in advance :)
edit: I realized that it would help to explain exactly what I am trying to do. There will be 100 check boxes and whenever one is clicked I would like to do this:
Call CheckBoxClicked("checkboxname")
Upvotes: 0
Views: 5507
Reputation:
Put this in a class moduled named clsCheckBoxHandler
Public WithEvents chk As MSForms.CheckBox
Private Sub chk_Click()
MsgBox chk.Caption & " Clicked!"
End Sub
then in the Userform
Dim chkCollection As Collection
Private Sub UserForm_Initialize()
Dim cCont As Control
Dim chkH As clsCheckBoxHandler
Set chkCollection = New Collection
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
Set chkH = New clsCheckBoxHandler
Set chkH.chk = cCont
chkCollection.Add chkH
End If
Next cCont
End Sub
this is just a simple handler for checkboxes that has a click event but can be extended to cover multiple controls and events.
Upvotes: 2