问题
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.
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"
回答1:
To deselect all the items in a list box
For clearlstbox = 0 To AOI.ListCount - 1
AOI.Selected(clearlstbox) = False
Next
回答2:
You can deselect any selected values in a listbox by running this:
Me.Listbox1.Value = ""
where "Listbox1" is the name of your listbox
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
来源:https://stackoverflow.com/questions/21055720/clear-items-selected-from-listbox