根据其值从列表框中删除项目VBA [英] Removing items from listbox according to their values VBA

查看:369
本文介绍了根据其值从列表框中删除项目VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除用户表单列表框中的所有项(特定值除外)

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"

我写道:

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?

推荐答案

使用向后循环:

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

and also change OR to AND in your IF statement

这篇关于根据其值从列表框中删除项目VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆