美好的一天,
我有以下代码
Sub AutFilCrit()
With Worksheets("Email Campaign Stats")
.AutoFilterMode = False
.Range("A6:AP6").AutoFilter
.Range("A6:AP6").AutoFilter Field:=5, Criteria1:=Array("OARGUELLO", "KARAUZ", "LBALLADARES"), Operator:=xlFilterValues
End With
End Sub
如您所见,条件是直接写入代码的名称数组。我的问题是:假设我有一个范围A1:A10或A1:K1的名单,我如何使用该范围作为标准的数组
答案 0 :(得分:2)
以下示例从 Sheet2 中的三个单元构建数组,以过滤 Sheet1 中的列:
Sub UsingARange()
Dim arr(1 To 3) As String
For i = 1 To 3
arr(i) = Sheets("Sheet2").Cells(i, 1).Value
Next i
With Sheets("Sheet1").Range("A1:A9")
.AutoFilter
.AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues
End With
End Sub
如果你想避免循环:
Sub UsingARange2()
Dim rng As Range
Set rng = Sheets("Sheet2").Range("A1:A3")
arr = Application.WorksheetFunction.Transpose(rng.Value)
With Sheets("Sheet1").Range("A1:A9")
.AutoFilter
.AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues
End With
End Sub