Coming from the question Excel macro filter based on multiple cell values and using the answer from Creating an Array from a Range in VBA
I was trying to create an autofilter for a wide range using an array. However, it keeps using only the latest value in the range of the array.
How can I use all of the values in the filter?
Sub Test()
Dim DirArray As Variant
DirArray = [A2:A5].Value2
ActiveSheet.range("$B$1:$C$10").AutoFilter Field:=2, Operator:=xlFilterValues, Criteria1:=DirArray
End Sub
Recording a Macro while applying a filter using multiple numeric value reveals that the filter is expecting an array of string.
ActiveSheet.Range("$B$1:$C$11").AutoFilter Field:=2, Criteria1:=Array("1", "2", "3"), Operator:=xlFilterValues
Notice that data = [A2:A5].Value
returns a 2D array
Let's flatten the 2D array: data = [A2:A5].Value
Now that we have a 1D array we need to convert it to an Array of String.
We can do this by first creating a delimited string
Data = Join(Data, ",")
And then Splitting that string
ActiveSheet.Range("$B$1:$C$11").AutoFilter Field:=2, Operator:=xlFilterValues, _ Criteria1:=Split(Join(Application.Transpose(Range("A2:A6")), ","), ",")
TextJoin()
really simplifies the process.
ActiveSheet.Range("$B$1:$C$11").AutoFilter Field:=2, Operator:=xlFilterValues, _ Criteria1:=Split(WorksheetFunction.TextJoin(",", True, Range("A2:A6")), ",")
Without seeing your data, I can see one conflicting issue in your code and the possibility of another conflict with your data.
The AutoFilter is expecting a simple 1-D array for Criteria1 with operator:=xlfiltervalues. You are passing in a 2-D array. Just use application.transpose to convert the incoming data from 2-D to 1-D.
DirArray = Application.Transpose(.Range("A2:A5").value2)
DirArray doesn't sound like a variable that was created to store numbers but it's worth mentioning that AutoFilter doesn't like an array of true numbers being passed into Criteria1; it expects text-that-look-like-numbers. After collecting the values, run through them and convert true numbers to text facsimiles.
DirArray = Application.Transpose(.Range("A2:A5").value2) For i = LBound(DirArray) To UBound(DirArray) DirArray(i) = CStr(DirArray(i)) Next i
So with those two issues dealt with, you should have no problem passing in an array as criteria.
Option Explicit
Sub Test()
Dim DirArray As Variant, i As Long
With ActiveSheet
If .AutoFilterMode Then .AutoFilterMode = False
DirArray = Application.Transpose(.Range("A2:A5"))
For i = LBound(DirArray) To UBound(DirArray)
DirArray(i) = CStr(DirArray(i))
Next i
.Range("$B$1:$C$10").AutoFilter Field:=2, Operator:=xlFilterValues, Criteria1:=DirArray
End With
End Sub
You can use this:
Sub arraytest2()
Dim DirArray As Variant
DirArray = ActiveSheet.Range("A1:A5")
ActiveSheet.Range("$B$1:$C$10").AutoFilter Field:=2, Operator:=xlFilterValues, Criteria1:=Array(ActiveSheet.Range("A" & LBound(DirArray) & ",A" & UBound(DirArray)))
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.