Background
This could end up being a rather silly question, however I wish to still ask it to be sure. As of recently I been introduced to AdvancedFilter through VBA, and reading the CriteriaRange parameter takes a Variant data type I thought it may have been able to take an array instead of an actual Range as the name suggests.
Sample Data
For this I have set up a simple data set:
| Hdr1 | Hdr2 | Hdr3 |
|------|------|------|
| A | X | 1 |
| B | Y | 2 |
| A | Z | 3 |
| A | Y | 4 |
| B | Y | 5 |
| A | Z | 6 |
This data sits on sheet with CodeName "Source". Besides that, I added a sheet called "Destination" as to where I want the filtered criteria to be pasted through xlFilterCopy.
Code
A simple working code would be:
Sub FilterTest()
Dim rng as range
Dim arr(1 To 2, 1 To 3) As Variant
arr(1, 1) = "Hdr1"
arr(1, 2) = "Hdr2"
arr(1, 3) = "Hdr3"
arr(2, 1) = "A"
'arr(2, 2) = "" 'Leaving empty for now
'arr(2, 3) = "" 'Leaving empty for now
Set rng = Destination.Range("A1:C2")
rng = arr
Source.Range("A1:C7").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rng, CopyToRange:=Destination.Range("A4")
End Sub
Problem/Curiosity
As the above works fine, I'm curious if there is a way to avoid filling the CriteriaRange prior to applying the AdvancedFilter, or even implement the arr array directly. As is, I believe we can't but I am curious to be proven wrong.
Whereas filling the evenly dimensioned rng with arr works, obviously the following line will error (1004) out:
Source.Range("A1:D7").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=arr, CopyToRange:=Destination.Range("A4")
The Variant data type for CriteriaRange seems to be expected (as per the documentation) but seemingly only the Range objects are actual working parameters.
Any definate answer as to whether it's possible? =)