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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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:
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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