使用For循环迭代数组中的每个条件,以便AutoFilter可以按照所有条件进行过滤。
示例代码:
Sub Filter_Data() Dim FilterArray As Variant Dim ws As Worksheet Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
FilterArray = Array("Vendor A", "Vendor B", "Vendor C") '多个条件
With ws
.Range("A1:D1").AutoFilter '打开AutoFilter
For i = LBound(FilterArray) To UBound(FilterArray) '循环条件并应用每个条件
.Range("D1").AutoFilter Field:=4, Criteria1:=FilterArray(i), Operator:=xlFilterValues
Next i
End With
End Sub