I have a userform with multiple checkboxes that fall within two types, with names that start with either "mm" or "pg". The code below (which is in a class module) successfully triggers when the userform that has the checkbox is displayed and userform code run manually from within the VBA editor and a checkbox is clicked to change the status.
The userform is set to launch automatically on file open, using a userform.show command in a workbook_open event sub. When the userform is opened in this way, it appears to work but the class module below does not appear to be triggered when a checkbox is clicked. I am assuming that there must be something in the way class modules work or some initialisation step that is needed on startup. I have not been able to find anything online to explain this behaviour, after a lot of looking, and suspect it is something blindingly obvious once you see it. If anyone has any ideas that would be most appreciated, it is getting very frustrating!
I should add that I have code in the userform that runs when the form is activated, which is:
oCtl is a public variable declared in a code module as follows:
Could the problem lie with the creation of the event handlers collection? I have to admit that it is pushing my knowledge of VBA when I created this code, it is not something I have ever needed to do before.
Thanks
Neil
The userform is set to launch automatically on file open, using a userform.show command in a workbook_open event sub. When the userform is opened in this way, it appears to work but the class module below does not appear to be triggered when a checkbox is clicked. I am assuming that there must be something in the way class modules work or some initialisation step that is needed on startup. I have not been able to find anything online to explain this behaviour, after a lot of looking, and suspect it is something blindingly obvious once you see it. If anyone has any ideas that would be most appreciated, it is getting very frustrating!
VBA Code:
Public WithEvents chkbox As msforms.checkbox
Public Property Set checkbox(ByVal c As msforms.checkbox)
Set chkbox = c
End Property
Private Sub chkbox_Change()
'this will run if any checkbox is clicked so the value changes
If Left(chkbox.name, 2) = "mm" Then
[code here]
End If
If Left(chkbox.name, 2) = "pg" Then
[code here]
End If
End Sub
I should add that I have code in the userform that runs when the form is activated, which is:
VBA Code:
Private Sub check_box_double_click_triggers()
'this is the code for generic checkbox double click triggers
Dim chkbox As click_detection
Set myEventHandlers = New Collection
For Each oCtl In introform.Controls
If TypeName(oCtl) = "CheckBox" Then
Set chkbox = New click_detection
Set chkbox.checkbox = oCtl
myEventHandlers.Add chkbox
End If
Next oCtl
End Sub
oCtl is a public variable declared in a code module as follows:
Public oCtl As Control
Could the problem lie with the creation of the event handlers collection? I have to admit that it is pushing my knowledge of VBA when I created this code, it is not something I have ever needed to do before.
Thanks
Neil
Last edited by a moderator: