Individual click events to dynamic checkboxes on userform, VBA

Ramachandran

New Member
Joined
Oct 17, 2011
Messages
47
I am populating a frame on a form with a number of checkboxes:

Userform_initialize()
...

Dim cB As MSForms.CheckBox

For i = 1 To dX
For j = 1 To dY

idx = j + (((i - 1) *dY))

Set cB = Frame2.Controls.Add("Forms.CheckBox.1", "chkBox" & idx)
With cB
.width = 50
.Height = 16
.Left = 6 + (54 * (j - 1))
.Top = 17 + (19 * (i - 1))
.Visible = True
End With

Next j
Next i

The frame is populated fine.
However I want individual click events for each of the chkBox'es.

Best case:
One clever multi event handler
private sub chkBoxClick(boxNumber as integer)
'Do stuff
end sub

or perhaps use some userform event:
private sub Userform_Clack()
If Left(me.activecontrol.name, 6).value = "chkBox' then...
end sub

Worst case:
I could create thousands single event handlers.
private sub chkBox173_Click()
Call LotsOfFun(173)
end sub
I can't even find a way to this...

Any suggestions?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Upvote 0
Thank you very much sir!
Pairing all of the checkboxes with a class that uses WithEvents didn't help much at first - only the last created checkbox had the desired event.
However when I created a collection and added every instances of the class in that collection, it all worked :)

class - clsBoxEvent:
Option Explicit
Public WithEvents cBox As MSForms.CheckBox

Private Sub cBox_Click()
MsgBox cBox.name​
End Sub

userform:
Dim chkBoxEvent As clsBoxEvent
Dim chkBox As MSForms.CheckBox
Dim chkBoxColl As Collection


Private Sub Userform_Initialize()

For i = 1 To dX​
For j = 1 To dY

idx = j + (((i - 1) * dY))

Set chkBox = Frame2.Controls.Add("Forms.CheckBox.1", "ChkBox" & idx)
With chkBox
...
End With

Set chkBoxEvent = New clsBoxEvent
Set chkBoxEvent.cBox = Me.Controls(chkBox.name)
chkBoxColl.Add chkBoxEvent

Next j
Next i
 
Upvote 0
Think that's the problem I had at first - getting it all into a collection. Glad it worked though.
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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