vba 清除从列表框中选择的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21055720/
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-12 01:21:14  来源:igfitidea点击:

Clear items selected from ListBox

excelvbaexcel-vbalistbox

提问by user3175856

I am trying to create a simple form using an Excel macro. This form will be used to input data which will be stored in Sheet2. Once the data has been saved, I would like to clear the contents from the form. I have been able to achieve this for most of the input boxes except for listbox. Below is the code that through which I am trying to achieve this functionality.

我正在尝试使用 Excel 宏创建一个简单的表单。此表单将用于输入将存储在 Sheet2 中的数据。保存数据后,我想清除表单中的内容。除了列表框之外,我已经能够为大多数输入框实现这一点。以下是我试图实现此功能的代码。

     Dim clearlstbox As Long

      With AOI
       For clearlstbox = .ListCount - 1 To 0 Step -1
         If .Selected(clearlstbox) = True Then
        .RemoveItem clearlstbox
         End If
       Next clearlstbox
      End With

   '
   'For clearlstbox = AOI.ListCount - 1 To 0 Step -1
   '    If AOI.Selected(clearlstbox) = True Then
   '        AOI.RemoveItem (clearlstbox)
   '    End If
   'Next

With both the codes it throws a similar error message "runtime error '-2147467259 (80004005) unspecified error"

使用这两个代码,它会抛出类似的错误消息“运行时错误 '-2147467259 (80004005) 未指定错误”

回答by Tmdean

To deselect all the items in a list box

取消选择列表框中的所有项目

For clearlstbox = 0 To AOI.ListCount - 1
    AOI.Selected(clearlstbox) = False
Next

回答by SilverShotBee

You can deselect any selected values in a listbox by running this:

您可以通过运行以下命令取消选择列表框中的任何选定值:

Me.Listbox1.Value = ""

where "Listbox1" is the name of your listbox

其中“Listbox1”是列表框的名称

To clear a multiselect listbox, use this code:

要清除多选列表框,请使用以下代码:

Me.listbox1.MultiSelect = fmMultiSelectSingle
Me.listbox1.Value = ""
Me.listbox1.MultiSelect = fmMultiSelectMulti

this sets it to a single select to clear it, then back to a multiselect for your original functionality

这将它设置为单个选择以清除它,然后返回到原始功能的多选