我需要为autofilter字段使用一串数据。在实际的宏中,字符串的创建更复杂和可变,但下面的示例更简单地重现了我的问题。
当我尝试将字符串变量用于自动过滤器时,自动过滤器会隐藏所有内容。当我使用相同的字符串但直接将其放入数组时,它可以工作。任何人都可以帮我解决如何使用数组进行过滤的问题吗?
Term = """" & APR & """" & "," & """" & MAY & """"
Debug.Print Term 'just to verify the strings are the same
Arr1 = Array("APR", "MAY") 'works in the filter
Arr1 = Array(Term) 'fails in the filter
Worksheets("Filter").Range("$A:$H").AutoFilter Field:=2, _
Criteria1:=Arr1, Operator:=xlFilterValues
答案 0 :(得分:0)
试试这个
Dim APR As String
Dim MAY As String
Dim arr1(0 To 1) As String
APR = "APR"
MAY = "MAY"
arr1(0) = APR
arr1(1) = MAY
Worksheets("Filter").Range("$A:$H").AutoFilter Field:=2, _
Criteria1:=arr1, Operator:=xlFilterValues