vba - 设置自动过滤多个通配符

标签 vba excel dictionary autofilter

现在我正在编写代码以设置数据图表的过滤器。基本上,我不知道如何在此处发布数据表,所以只需尝试输入它们):

(从左边开始是A列)
名称 * BDevice * 数量 * 销售* 所有者

基本上我需要过滤掉2列:
- 任何单词的BDevice包含“M1454”或“M1467”或“M1879”(这意味着M1454A或M1467TR仍然适合)
- 有生产或风险的所有者

这是我写的代码:

Sub AutoFilter()

  ActiveWorkbook.ActiveSheet..Range(B:B).Select

  Selection.Autofilter Field:=1 Criteria1:=Array( _
      "*M1454*", "*M1467*", "*M1879*"), Operator:=xlFilterValues

  Selection.AutoFilter Field:=4 Criteria1:="=PROD" _
      , Operator:=xlOr, Criteria2:="=RISK"

End Sub

当我运行代码时,机器返回错误1004,似乎错误的部分是Filter part 2(我不确定Field的使用,所以我不能确定)

编辑;桑托什:当我尝试您的代码时,机器出现错误 9 下标超出范围。错误来自 with 语句。 (由于数据表有 A 到 AS 列,所以我只更改为 A:AS)

最佳答案

AutoFilter method 中的每个字段最多有两个直接通配符。 , 模式匹配可用于创建将通配符替换为 的数组。运算符:=xlFilterValues 选项。一个 Select Case statement帮助通配符匹配。

第二个字段是简单的 Criteria1 和 Criteria2 直接匹配 运算符:=xlOr 加入这两个标准。

Sub multiWildcardFilter()
    Dim a As Long, aARRs As Variant, dVALs As Object

    Set dVALs = CreateObject("Scripting.Dictionary")
    dVALs.CompareMode = vbTextCompare

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            'build a dictionary so the keys can be used as the array filter
            aARRs = .Columns(2).Cells.Value2
            For a = LBound(aARRs, 1) + 1 To UBound(aARRs, 1)
                Select Case True
                    Case aARRs(a, 1) Like "MK1454*"
                        dVALs.Add Key:=aARRs(a, 1), Item:=aARRs(a, 1)
                    Case aARRs(a, 1) Like "MK1467*"
                        dVALs.Add Key:=aARRs(a, 1), Item:=aARRs(a, 1)
                    Case aARRs(a, 1) Like "MK1879*"
                        dVALs.Add Key:=aARRs(a, 1), Item:=aARRs(a, 1)
                    Case Else
                        'no match. do nothing
                End Select
            Next a

            'filter on column B if dictionary keys exist
            If CBool(dVALs.Count) Then _
                .AutoFilter Field:=2, Criteria1:=dVALs.keys, _
                                      Operator:=xlFilterValues, VisibleDropDown:=False
            'filter on column E
            .AutoFilter Field:=5, Criteria1:="PROD", Operator:=xlOr, _
                                  Criteria2:="RISK", VisibleDropDown:=False

            'data is filtered on MK1454*, MK1467* or MK1879* (column B)
            'column E is either PROD or RISK
            'Perform work on filtered data here
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With

    dVALs.RemoveAll: Set dVALs = Nothing
End Sub

如果要将排除项¹添加到过滤中,则它们的逻辑应放在 Select..End Select 语句的顶部,以便它们不会通过误报添加到其他匹配条件。

multi_Wildcard_Filter_Before应用自动筛选方法之前

multi_Wildcard_Filter_After应用带有多个通配符的自动筛选后

¹ 见 Can Advanced Filter criteria be in the VBA rather than a range?Can AutoFilter take both inclusive and non-inclusive wildcards from Dictionary keys?有关向字典的过滤器集添加排除项的更多信息。

关于vba - 设置自动过滤多个通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116041/

相关文章:

VBA 替换不适用于 Excel 文本文档的代码

excel - 运行时错误 '28' : Out of stack space in Excel VBA and Excel crashes

.net - 使用 .ToDictionary 将 List(Of KeyValuePair(Of String,Int32)) 转换为 Dictionary(Of String, Int32)

python - 根据时间对字典列表值进行排序

vba - 如何检查单元格值是否大于 X,获取这两个值的差异并添加到 Excel 中的下一个单元格?

vba - 在 VBA 中追加动态数组

vba - UserForm绘制后自动执行一些代码

excel - 包括空白单元格的平均值

python - 如何在 python xlsxwriter 中设置 invert_if_negative 以将条形填充为纯色

Python 字典和列表混淆