vba 从组合框中删除所有项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2998161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:31:15  来源:igfitidea点击:

Removing All Items From A ComboBox?

vbacombobox

提问by sooprise

How can I programmatically remove all items from a combobox in VBA?

如何以编程方式从 VBA 的组合框中删除所有项目?

回答by Tommy

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.

基本上,当您有项目时,从组合框中删除第一个项目。删除所有项目后(计数 = 0),您的框是空白的。

Method 2: Even better

方法二:更好

ComboBox1.Clear

回答by Robben_Ford_Fan_boy

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 

回答by Hendry Halim

The simplest way:

最简单的方法:

Combobox1.RowSource = ""  'Clear the list
Combobox1.Clear           'Clear the selected text

回答by David

You can use the ControlFormat method:

您可以使用 ControlFormat 方法:

ComboBox1.ControlFormat.RemoveAllItems

回答by Mr_Hmp

Best Way:

最好的办法:

Combobox1.items.clear();

回答by Tim Wray

For Access VBA, which does not provide a .clear method on user form comboboxes, this solution works flawlessly for me:

对于未在用户表单组合框上提供 .clear 方法的 Access VBA,此解决方案对我来说完美无缺:

   If cbxCombobox.ListCount > 0 Then
        For remloop = (cbxCombobox.ListCount - 1) To 0 Step -1
            cbxCombobox.RemoveItem (remloop)
        Next remloop
   End If

回答by Henning Lee

me.Combobox1.Clear

me.Combobox1.Clear

This is the common method

这是常用的方法

回答by Andrew Watkins

I could not get clear to work. (Mac Excel) but this does.

我无法清楚地工作。(Mac Excel) 但确实如此。

ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems

ActiveSheet.DropDowns("CollectionComboBox").RemoveAllItems

回答by Pat Harkin

In Access 2013 I've just tested this:

在 Access 2013 中,我刚刚测试了这个:

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.

有趣的是,如果你在属性中设置了项目列表,当你退出表单视图并返回到设计视图时,这并不会丢失。

回答by Robert

Private Sub cmdClear_Click()
    ComboBox1.Value = Null
    ComboBox2.Value = Null
End Sub