How to run the procedure after clicking on a combobox that was created dynamically

aivin

New Member
Joined
Mar 29, 2024
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
I have a userform with a sample combobox list like this:
VBA Code:
Private Sub UserForm_Initialize()
Dim CooBox As MSForms.ComboBox, CooBoxPoz As String
Dim x As Integer

For x = 1 To 10
    CooBoxPoz = "CooBox" & x
    Set CooBox = UserForm1.Controls.Add("Forms.Combobox.1", CooBoxPoz, True)
    Call DynamicForms(CooBox, 90, 34 + ((x - 1) * 20), 126)
    UserForm1.Controls(CooBoxPoz).AddItem "Test1"
    UserForm1.Controls(CooBoxPoz).AddItem "Test2"
Next

End Sub

Sub DynamicForms(ByRef Element As MSForms.Control, ElLeft As Long, _
                 ElTop As Long, ElWidth As Long, Optional ElCaption As String)

'On Error Resume Next
If ElCaption <> "" Then
    Element.Caption = ElCaption
End If
Element.Left = ElLeft
Element.Top = ElTop
Element.Width = ElWidth

End Sub

Each CooBox1-10 comobox created has two items Test1 and Test2. I would like the procedure to be launched after clicking on any CooBox and changing from Test1 to Test2. The following example doesn't work. What am I doing wrong?

VBA Code:
Private Sub CooBox3_Click()
MsgBox "3 click"
End Sub
Private Sub CooBox3_Change()
MsgBox "3 change"
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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