如何在VBA中以编程方式从组合框中删除所有项?
以下为伪代码(已更新为实际代码):
Do While ComboBox1.ListCount > 0
ComboBox1.RemoveItem (0)
Loop
基本上,只要ComboBox中还有项,就删除第一项。当所有项都被删除(计数=0)时,您的ComboBox就为空。
方法2:更好的做法
ComboBox1.Clear
不幸的是,您需要逐个删除每个项目:
For i = 1 To ListBox1.ListCount
'Remove an item from the ListBox using ListBox1.RemoveItem
Next i
更新 - 我不知道为什么我的答案没有包含完整的解决方案:
For i = ListBox1.ListCount - 1 to 0 Step - 1
ListBox1.RemoveItem i
Next i
For i = ListBox1.ListCount to 1 Step -1
,这样应该可以正常工作。 - ptpatersonFor i = ListBox1.ListCount - 1 to 0 Step -1 ListBox1.RemoveItem i Next
。 - ptpaterson最简单的方法:
Combobox1.RowSource = "" 'Clear the list
Combobox1.Clear 'Clear the selected text
ComboBox1.ControlFormat.RemoveAllItems
最佳方式:
Combobox1.items.clear();
对于Access VBA,如果ComboBox已经使用Value List的Row Source Type填充,则我发现以下内容有效:
ComboBox.RowSource = ""
If cbxCombobox.ListCount > 0 Then
For remloop = (cbxCombobox.ListCount - 1) To 0 Step -1
cbxCombobox.RemoveItem (remloop)
Next remloop
End If
While ComboBox1.ListCount > 0
ComboBox1.RemoveItem 0
Wend
me.Combobox1.Clear
这是常用的方法
我无法让“清除”(Mac Excel)起作用,但这个可以。
ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems
.Clear
方法,但是lst.RowSource = vbNullString
可以删除所有项目。否则,Do ... Loop
对我来说似乎是最好的解决方法。 - martin.lindenlauf