Reputation: 606
I was wondering how to add items to a combobox with a few drop down selections. I have attached my code below but nothing showed up. Any advise?
Sub ComboBox1_Change()
Dim i As Integer
i = ComboBox1.Items.Add("This is a new item")
ComboBox1.SelectedIndex = i
End Sub
Upvotes: 1
Views: 495
Reputation: 149297
You need to use .AddItem
instead of .Items.Add
and also no point adding item in combo's change event. I am using a command button to add to it.
Is this what you are trying?
Private Sub CommandButton1_Click()
ComboBox1.AddItem "This is a new item"
'~~> After adding the item this will display the
'~~> last item added in the combobox
ComboBox1.Value = ComboBox1.List(ComboBox1.ListCount - 1)
'~~> Display the dropdown via code if you need this as well
ComboBox1.DropDown
End Sub
If you want to add items directly from the combobox then you can use this as well. This will add items after you enter something in the combobox and press ENTER key
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'~~> On Key Enter
If KeyCode = 13 Then
ComboBox1.AddItem "This is a new item"
End If
End Sub
Upvotes: 2