Class module only appears to work when userform code is run from the editor

nabuzzard

New Member
Joined
Feb 8, 2008
Messages
9
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!
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:

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,414
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Where is myEventHandlers declared? It would probably help if you post the code for your Userform_Activate and Userform_Initialize events.
 

nabuzzard

New Member
Joined
Feb 8, 2008
Messages
9
Thanks for the resposne.

The userform activate code is in the userform code section which starts as follows:

Option Explicit
Private myEventHandlers As Collection

sub UserForm_Activate()

Event_Enabled = True
text_box_double_click_triggers

'set as full screen
Application.WindowState = xlMaximized

End Sub


I do not have a userform sub, could that be where the problem lies?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,414
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You don't appear to be running the check_box_double_click_triggers routine in that code.
 
Solution

nabuzzard

New Member
Joined
Feb 8, 2008
Messages
9
Fair point. I think I did not spot this because it worked when activated via the vba editor. An earlier iteration of the code was using text boxes, hence the legacy error.

This appears to have fixed matters, but I am not sure I understand how that would change when the code was run from the editor to via the workbook_open event?

Anyway, apologies for wasting your time with something I should have spotted, too much time staring at code and missed the glaring error.

Thanks
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,414
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Not to worry - glad it's fixed. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,126,952
Messages
5,621,797
Members
415,856
Latest member
jimb2k

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
Top