问题
I'm trying to delete all the items in a user form list-box except for specific values
lets say I want to delete everything in my list-box except "Cat" and "Dog"
I wrote:
For i = 0 To ListBox2.ListCount - 1
If ListBox2.List(i) <> "Cat" or ListBox2.List(i) <> "Dog" Then
ListBox2.RemoveItem i
End If
Next
For some reason it doesn't work, I tried to find a solution but I couldn't. What is wrong here?
回答1:
Use backwards loop:
For i = ListBox2.ListCount - 1 To 0 Step -1
If ListBox2.List(i) <> "Cat" AND ListBox2.List(i) <> "Dog" Then
ListBox2.RemoveItem i
End If
Next
and also change OR
to AND
in your IF
statement
来源:https://stackoverflow.com/questions/23215035/removing-items-from-listbox-according-to-their-values-vba