简体   繁体   中英

Autofilter with multiple criteria using an array of ranges

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.

  1. 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) 
  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM