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
 

Some videos you may like

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,742
Office Version
365
Platform
Windows
Richard

What type of comboboxes are you dealing with?

Perhaps you should look into using a class module to handle the events.
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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:
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,316
Messages
5,510,554
Members
408,798
Latest member
jitu20feb

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top