设置自动筛选多个通配符

现在我正在做编码设置数据图表的filter。 基本上,我不知道如何在这里发布数据表,所以只要input它们即可):

(从左边开始是列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第2部分(我不确定使用的字段,所以我不能肯定地说)

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

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

第二个字段是一个简单的Criteria1和Criteria2与运算符的直接匹配:= xlOrjoin了两个条件。

 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 

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

multi_Wildcard_Filter_Before
应用AutoFilter方法之前

multi_Wildcard_Filter_After
应用带有多个通配符的AutoFilter后


¹ 请参阅高级筛选条件是否可以在VBA而不是范围内? 并且AutoFilter可以从Dictionary键中同时使用包含和不包含的通配符? 有关向字典的filter集添加排除的更多信息。

尝试下面的代码:

Criteria1工作的最大2通配符expression式。 参考这个链接

 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 

对于使用部分string排除行和包含空白,您应该使用“从Jeeped的代码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的其他答案在这里多个filter标准的空白和数字在同一领域使用通配符不起作用