Excel VBA Autofilter包含多个条件 [英] Excel VBA Autofilter contains with multiple criteria

查看:5669
本文介绍了Excel VBA Autofilter包含多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用运算符过滤带有多个条件的范围包含

I need to filter a range with multiple criteria with operator Contains

以下代码工作得很好(2 critera) :

shData.UsedRange.AutoFilter field:=2, Criteria1:=Array("*a*", "*b*"), Operator:=xlFilterValues

但是,代码不起作用(超过2个条件):

shData.UsedRange.AutoFilter field:=2, Criteria1:=Array("*a*", "*b*", "*c*"), Operator:=xlFilterValues

任何人都可以请求帮助?

Anybody can please help on this?

谢谢。

推荐答案

这是自动过滤器的限制。您不能在UI中使用两个以上的包含过滤器。

It's a limitation of autofilters. You can't use more than two contains filters in the UI either.

您可以使用高级过滤器,也可以创建一组数值符合您的条件并使用以下过滤器:

You can either use an Advanced filter instead, or you can create an array of the values that match your criteria and filter using that:

Sub MultiContainsAutofilter()
    Dim vData
    Dim shData                As Worksheet
    Dim d                     As Object
    Dim i                     As Long

    Set shData = ActiveSheet
    vData = shData.UsedRange.Columns(2)

    Set d = CreateObject("Scripting.Dictionary")

    For i = LBound(vData, 1) To UBound(vData, 1)
        If UCase$(vData(i, 1)) Like "*A*" Or UCase$(vData(i, 1)) Like "*B*" Or UCase$(vData(i, 1)) Like "*C*" Then
            d(vData(i, 1)) = Empty
        End If
    Next i

    If d.Count > 0 Then shData.UsedRange.AutoFilter Field:=2, Criteria1:=d.keys, Operator:=xlFilterValues
End Sub

这篇关于Excel VBA Autofilter包含多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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