现在我正在编码为数据图表设置过滤器。基本上,我不知道如何在这里发布数据表,所以只需尝试输入它们:
(从左边开始是A栏) 姓名* BDevice *数量*销售*所有者
基本上我需要过滤掉2列: - 任何单词的BDevice包含“M1454”或“M1467”或“M1879”(这意味着M1454A或M1467TR仍然适合) - 拥有PROD或风险的所有者
这是我写的代码:
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并且看起来错误的部分是过滤器部分2(我不确定使用Field,所以我不能肯定地说)
修改; Santosh:当我尝试你的代码时,机器得到错误9下标超出范围。错误来自with语句。 (因为数据表有A到AS列,所以我只改为A:AS)
答案 0 :(得分:7)
虽然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语句的顶部,以便它们不会通过误报添加到其他匹配条件中。
¹有关向字典的过滤器集添加排除项的详情,请参阅Can Advanced Filter criteria be in the VBA rather than a range?和Can AutoFilter take both inclusive and non-inclusive wildcards from Dictionary keys?。
答案 1 :(得分: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
答案 2 :(得分:0)
尝试以下代码:
Criteria1的最大2个通配符表达式有效。请参阅此link
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