我试图通过查看一个单元格是否包含其中一个名称来筛选我的表,当我使用theFor时,它会在一行中显示所有数据。
代码如下:
Dim tab(3) as string '
'tab(0) = "*valerie dupond*"'
'tab(1) = "*emanuel babri*"'
'tab(2) = "*raphael gerand*"'
For i = 0 To 2
'Worksheets("Feuil1").Range("A1").AutoFilter field:=2, Criteria1:=tab , ''Operator:=xlFilterValues'
'Next'发布于 2019-05-19 20:23:13
你遇到的问题是,当你过滤一个数组时,你不能使用通配符("/*")。
绕过这一限制是困难的,但并非不可能。我以前做过的方法是这样做:
1)将您要筛选的列(我想是第2列)中的所有值复制到一个空白工作表中。
2)去重。
3)遍历所有剩余行,并删除任何不符合条件的行。
4)将剩余的值放入数组中。
5)过滤该数组上的原始数据。
我不能访问代码,但它类似于下面的内容。我没有测试过它。我现在不在装有Excel的计算机上,所以您必须清理它,修复错误,并在Visual Basic中启用正则表达式。应该在Tools->References菜单中。您还可以对此代码稍加修改,以对其进行操作。
Dim i As Integer
Dim c As Integer
Dim lRow As Integer
Dim regEx As New RegExp
Dim rEx As String
Dim arr(1) As String
lRow = Range(shSheet.Rows.Count, ActiveCell.Column).End(xlup).Row 'Get's the last row of the current column selected so make sure to select the column you are trying to filter.
rEx = "^.*(valerie dupond|emanuel babri|raphael gerand).*$" ' The test string for the Regular Expression to match
'Setting up the Regular Expression.
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
i = 0 'Sets i to be looped through your values.
c = 1 'C will be set to store the values in the array.
'Loops through every row in your table trying to match the pattern above
For i to lRow
If regEx.Test(LCase(ActiveCell.Value)) Then
arr(c) = ActiveCell.Value
c = c + 1
ReDim Preserve arr(c)
End If
ActiveCell.Offset(1,0).Select
Next i
'Sets the filter
Worksheets("Feuil1").Range("A1").AutoFilter field:=2, Criteria1:=arr , ''Operator:=xlFilterValues方法2:
两点:
FOR循环你不需要循环。Criteria1=tab将过滤所有条件,不需要循环
代码2
只需删除通配符。例如,如果您只需要匹配"valerie dupond“而不是”valerie dupond夫人“
Sub FilterMe()
Dim names(3) As String
names(0) = "valerie dupond"
names(1) = "emanuel babri"
names(2) = "raphael gerand"
Worksheets("Feuil1").Range("A1").AutoFilter field:=2, Criteria1:=names, Operator:=xlFilterValues
End Sub同样,您不能使用自动筛选来筛选超过两个带有通配符的术语
https://stackoverflow.com/questions/56207459
复制相似问题