How can I programmatically remove all items from a开发者_StackOverflow社区 combobox in VBA?
Psuedo code ahead (updated with actual code):
Do While ComboBox1.ListCount > 0
ComboBox1.RemoveItem (0)
Loop
Basically, while you have items, remove the first item from the combobox. Once all the items have been removed (count = 0), your box is blank.
Method 2: Even better
ComboBox1.Clear
You need to remove each one individually unfortunately:
For i = 1 To ListBox1.ListCount
'Remove an item from the ListBox using ListBox1.RemoveItem
Next i
Update - I don't know why my answer did not include the full solution:
For i = ListBox1.ListCount - 1 to 0 Step - 1
ListBox1.RemoveItem i
Next i
The simplest way:
Combobox1.RowSource = "" 'Clear the list
Combobox1.Clear 'Clear the selected text
You can use the ControlFormat method:
ComboBox1.ControlFormat.RemoveAllItems
Best Way:
Combobox1.items.clear();
For Access VBA, which does not provide a .clear method on user form comboboxes, this solution works flawlessly for me:
If cbxCombobox.ListCount > 0 Then
For remloop = (cbxCombobox.ListCount - 1) To 0 Step -1
cbxCombobox.RemoveItem (remloop)
Next remloop
End If
For Access VBA, if a ComboBox has been populated with a Row Source Type of Value List, I find the following works:
ComboBox.RowSource = ""
In Access 2013 I've just tested this:
While ComboBox1.ListCount > 0
ComboBox1.RemoveItem 0
Wend
Interestingly, if you set the item list in Properties, this is not lost when you exit Form View and go back to Design View.
me.Combobox1.Clear
This is the common method
I could not get clear to work. (Mac Excel) but this does.
ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems
If you want to simply remove the value in the combo box:
me.combobox = ""
If you want to remove the recordset of the combobox, the easiest way is:
me.combobox.recordset = ""
me.combobox.requery
Private Sub cmdClear_Click()
ComboBox1.Value = Null
ComboBox2.Value = Null
End Sub
精彩评论