9

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

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

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

这是我写的代码:

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的使用,所以我不能确定)

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

4

3 回答 3

8

虽然AutoFilter 方法中每个字段最多有两个直接通配符,但模式匹配可用于创建一个数组,用Operator:=xlFilterValues选项替换通配符。Select Case 语句有助于通配符匹配。

第二个字段是简单的 Criteria1 和 Criteria2 直接匹配,使用Operator:=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
                        应用带有多个通配符的自动筛选后


¹请参阅高级筛选条件可以在 VBA 中而不是在范围内吗?并且AutoFilter 可以从 Dictionary 键中获取包含和非包含通配符吗?有关向字典的过滤器集添加排除项的更多信息。

于 2016-01-16T02:37:40.060 回答
1

对于使用部分字符串来排除行并包含空格,您应该使用

'From Jeeped's code
Dim dVals As Scripting.Dictionary
Set dVals = CreateObject("Scripting.Dictionary")
dVals.CompareMode = vbTextCompare    


Dim col3() As Variant
Dim col3init As Integer

'Swallow row3 into an array; start from 1 so it corresponds to row
For col3init = 1 to Sheets("Sheet1").UsedRange.Rows.count
    col3(col3init) = Sheets("Sheet1").Range(Cells(col3init,3),Cells(col3init,3)).Value
Next col3init

Dim excludeArray() As Variant
'Partial strings in below array will be checked against rows
excludeArray = Array("MK1", "MK2", "MK3")

Dim col3check As Integer
Dim excludecheck as Integer
Dim violations As Integer
For col3check = 1 to UBound(col3)
    For excludecheck = 0 to UBound(excludeArray) 
         If Instr(1,col3(col3check),excludeArray(excludecheck)) <> 0 Then
             violations = violations + 1
             'Sometimes the partial string you're filtering out for may appear more than once.
         End If
    Next col3check

    If violations = 0 and Not dVals.Exists(col3(col3check)) Then
         dVals.Add Key:=col3(col3check), Item:=col3(col3check) 'adds keys for items where the partial strings in excludeArray do NOT appear
    ElseIf col3(col3check) = "" Then
         dVals.Item(Chr(61)) = Chr(61) 'blanks
    End If
    violations = 0
Next col3check    

dVals.Item(Chr(61)) = Chr(61) 的想法来自 Jeeped 的另一个答案,这里 Multiple Filter Criteria for blanks and numbers using wildcard on same field just doesn't work

于 2016-10-14T11:43:24.130 回答
0

试试下面的代码:

Criteria1 的最大 2 个通配符表达式有效。参考这个链接

Sub AutoFilter()

    With ThisWorkbook.Sheets("sheet1").Range("A:E")
        .AutoFilter Field:=2, Criteria1:=Array("*M1454*", "*M1467*"), Operator:=xlFilterValues
        .AutoFilter Field:=5, Criteria1:="=PROD", Operator:=xlOr, Criteria2:="=RISK"
    End With

End Sub
于 2013-05-17T06:52:51.397 回答