Class of Comboboxes in UserForm

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I have a userform with 5 comboboxes that I want to add to a class module so that my code runs whenever a box value is changed. I have this so far:

On Userform Initialize:

Code:
'Assign all Comboboxes to ComboBoxes class module so that if any Combobox is clicked, _
    Class module code will run
    Dim ComboBoxControl As Control
    Dim ComboBoxClass As Class_ComboBoxes
    Set ComboBoxCollection = New Collection
    For Each ComboBoxControl In Me.Controls
        If TypeName(ComboBoxControl) = "ComboBox" Then
            Set ComboBoxClass = New Class_ComboBoxes
            Set ComboBoxClass.ComboBox = ComboBoxControl
            ComboBoxCollection.Add ComboBoxClass
        End If
    Next ComboBoxControl

In Class Module ("Class_Comboboxes")

Code:
Public WithEvents ComboBox As MSForms.ComboBox
Private Sub ComboBox_Change()
    MsgBox "changed"
End Sub

When the userform first initializes, I get a message box "Changed" for each one as they get populated. But after that, the message box does not appear when I change one of their values. I must be missing something?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

VBA Geek

MrExcel MVP
Joined
Dec 16, 2013
Messages
2,857
in the userform

Code:
Private cComboBoxHandler() As clsCombo


Private Sub UserForm_Initialize()
Dim Cntrl As MSForms.Control, K As Long
For Each Cntrl In Me.Controls
    If TypeOf Cntrl Is MSForms.ComboBox Then
        ReDim Preserve cComboBoxHandler(0 To K) As clsCombo
        Set cComboBoxHandler(K) = New clsCombo
        Set cComboBoxHandler(K).MyComboBox = Cntrl
        K = 1 + K
    End If
Next Cntrl
End Sub


in the class (called clsCombo)

Code:
Private WithEvents cmbBox As MSForms.ComboBox


Private Sub cmbBox_Change()
    MsgBox "Changed"
End Sub


Property Set MyComboBox(CMB As MSForms.ComboBox)
    Set cmbBox = CMB
End Property
 
Last edited:
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,015
Messages
5,834,939
Members
430,327
Latest member
Mr_Himalayan778

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