VBA:部分数组string的自动筛选

我需要为我所做的数组过滤一列。 我试图使用Cells.AutoFilter Field:=14, Criteria1:=stringArray, Operator:=但我不知道运营商应该是什么。

我的问题的一个例子是,当我自动过滤列中的内容实际上是“Tawm”时,我的Array中的某些内容可能是“Ta”。 我在想,像Operator:=xlContains但这是一个不行。

我只是想让它像我在“Ta”中input,然后select自动filterfind的所有选项。

我已经尝试添加"*"到数组中的每个条目,下面的代码,但它似乎没有帮助:

 Dim stringArray As Variant Dim tempMfr As String Dim temp2Mfr As String Dim t As Variant tempMfr = xCell & "*" temp2Mfr = xCell.Offset(0, 2) 'this cell may have multiple entries such as "a, b, c" stringArray = Split(temp2Mfr, ", ") For Each t In stringArray t = t & "*" Cells.AutoFilter Field:=14, Criteria:=stringArray, Operator:=xlFilterValues 

有一个更好的方法吗?

谢谢。

你不能在两个以上的AutoFilter方法标准中使用通配符,

 .AutoFilter Field:=1, Criteria1:="=abc*", Operator:=xlAnd, Criteria2:="=def*" ' or, .AutoFilter Field:=1, Criteria1:="=abc*", Operator:=xlOr, Criteria2:="=def*" 

将string拆分为variables数组后,只需遍历数组的元素并在每个元素上添加一个星号(例如Chr(42) )即可。

 Sub Macro3() Dim v As Long, vFilters As Variant, sFilters As String Dim xCell As Range With Sheets("Sheet1") Set xCell = .Range("ZZ99") sFilters = xCell.Offset(0, 2) 'this cell may have multiple entries such as "a, b, c" vFilters = Split(sFilters, ", ") With .Cells(1, 1).CurrentRegion For v = LBound(vFilters) To LBound(vFilters) .Cells.AutoFilter Field:=14, Criteria:=vFilters(v) & Chr(42) 'begins with filter 'move down a row to save the header and resize -1 row With .Resize(.Rows.Count - 1, .Columns.Count).offswet(1, 0) 'check if there is anything visible If CBool(Application.Subtotal(103, .Columns(14))) Then 'do something with '.Cells.SpecialCells (xlCellTypeVisible) End If End With Next v End With End With End Sub 

如果您同时需要两个以上的“通配符”条件,则可以使用大量的编码来构build数组以传递到filter,然后使用Operator:=xlFilterValues选项。