Add event handling for combobox created at runtime

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
I've been playing around with adding ComboBoxes at run time (eg in response to user actions on a sheet) and this I can easily manage. However, I was wondering how I can then get this newly created combobox itself to respond to user events eg user selecting a value, user clicking out of the combobox etc? Have I missed something simple?

Thanks for all replies!

Richard
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Here is an example:

Code:
Sub Test()
    Dim oole As OLEObject
    Dim CodeMod As Object
    Dim Code As String
    Dim Line As Integer
    Set oole = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, _
        DisplayAsIcon:=False, Left:=231.75, Top:=54.75, Width:=69.75, Height _
        :=24)
    MsgBox TypeName(oole)
    With oole.Object
        .List = Array("a", "b", "c", "d")
    End With
    Set CodeMod = ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
    With CodeMod
        Line = .CreateEventProc("Change", oole.Name) + 1
        Code = "   MsgBox ""You chose "" & " & oole.Name & ".Value"
        .InsertLines Line, Code
    End With
End Sub
 
Upvote 0
Richard

What type of comboboxes are you dealing with?

Perhaps you should look into using a class module to handle the events.
 
Upvote 0
Thanks Andrew - exactly what I was after :-D

Thanks Norie - using class modules is a very good idea, and one which I shall investigate.

:-D
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

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