Removing items from listbox according to their values VBA

我的梦境 提交于 2019-12-11 11:53:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!