Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and populate an activex combobox using vba in excel.

I am having a problem when trying to create and then populate a activex combobox in vba for excel. The code below works when run as two seperate macros but when I try to put the two together an empty combobox is created. Can anybody tell me why this is and how to overcome this problem?

Thanks in advance, JW

 Sub CreateComboBox1()
    'Creating ComboBox1:
    ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
                Link:=False, DisplayAsIcon:=False, Left:=50, Top:=80, Width:=100, _
                Height:=15).Select
    End Sub

    Sub PopulateComboBox1()
    'Populating ComboBox1
    Sheet1.ComboBox1.AddItem "Date", 0
    Sheet1.ComboBox1.AddItem "Player", 1
    Sheet1.ComboBox1.AddItem "Team", 2
    Sheet1.ComboBox1.AddItem "Goals", 3
    Sheet1.ComboBox1.AddItem "Number", 4
    End 
like image 337
James Watts Avatar asked Nov 29 '25 04:11

James Watts


1 Answers

Try this

Sub CreateComboBox1()
    With ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
                Link:=False, DisplayAsIcon:=False, Left:=50, Top:=80, Width:=100, _
                Height:=15)
        With .Object
            .AddItem "Date"
            .AddItem "Player"
            .AddItem "Team"
            .AddItem "Goals"
            .AddItem "Number"
        End With
    End With
End Sub
like image 159
Siddharth Rout Avatar answered Nov 30 '25 21:11

Siddharth Rout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!