在这个小示例中,我们从 Info 选项卡中收集值,删除所有空白并将该过滤器应用于 Data 选项卡的第一列,以便显示所有匹配项:
Sub luxation()
Dim sh1 As Worksheet, N As Long
Dim st As String
Set sh1 = Sheets("Info")
N = sh1.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To N
v = sh1.Cells(i, 1).Value
If v <> "" Then
st = st & v & ","
End If
Next i
st = Mid(st, 1, Len(st) - 1)
Arr1 = Split(st, ",")
Sheets("Data").AutoFilterMode = False
With Sheets("Data").Range("$A$1:$C$9244")
.AutoFilter Field:=1, Criteria1:=Arr1, Operator:=xlFilterValues
End With
End Sub
在下一个示例中,过滤器值按顺序应用:
Sub luxation2()
Dim sh1 As Worksheet, N As Long
Dim st As String
Set sh1 = Sheets("Info")
N = sh1.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To N
v = sh1.Cells(i, 1).Value
If v <> "" Then
st = st & v & ","
End If
Next i
st = Mid(st, 1, Len(st) - 1)
Arr1 = Split(st, ",")
Sheets("Data").Activate
For i = LBound(Arr1) To UBound(Arr1)
Sheets("Data").AutoFilterMode = False
With Sheets("Data").Range("$A$1:$C$9244")
.AutoFilter Field:=1, Criteria1:=Arr1(i), Operator:=xlFilterValues
End With
MsgBox "Check out the filter"
Next i
End Sub