根据其值VBA删除列表框中的项目

我试图删除除特定值以外的用户表单列表框中的所有项目

可以说我想删除我的列表框中除了“猫”和“狗”以外的所有东西

我写了:

For i = 0 To ListBox2.ListCount - 1 If ListBox2.List(i) <> "Cat" or ListBox2.List(i) <> "Dog" Then ListBox2.RemoveItem i End If Next 

由于某种原因,它不工作,我试图find一个解决scheme,但我不能。 这里有什么问题?

使用向后循环:

 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 

并在您的IF声明中将OR更改为AND