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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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 :biggrin:

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

:biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

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