Programmatically add ComboBox in VBA (Excel)
combobox, excel, vba
Solution
You can rely on the following code:
Set curCombo = ActiveSheet.Shapes.AddFormControl(xlDropDown, Left:=Cells(1, 1).Left, Top:=Cells(2, 1).Top, Width:=100, Height:=20)
With curCombo
.ControlFormat.DropDownLines = 2
.ControlFormat.AddItem "Item 1", 1
.ControlFormat.AddItem "item 2", 2
.Name = "myCombo"
.OnAction = "myCombo_Change"
End With
Problem
I am trying to create, place and fill in elements of a ComboBox in VBA. I don't understand what I am missing, but I cannot possibly figure out how to do it. I do not have `MSForms` in my API (i.e. I cannot write `Dim someComboBox As MSForms.ComboBox` as it fails. Is it possible to import it somehow? Can someone point me in the right direction? What I want to achieve is that when the user clicks a button a new form is created with various items (ComboBoxes, RadioButtons, etc.) and thus I want to do this programmatically.