Advanced Filter Criteria

jstevens
GoldLounger
Posts: 2640
Joined: 26 Jan 2010, 16:31
Location: Southern California

Advanced Filter Criteria

Post by jstevens »

Is it possibe to declare the "critereia range" as a VBA variable? I know how to use VBA to extract data using advanced filters but thought that this may be an interesting alternative.

Example of code:
oRng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=ThisWorkbook.Sheets("Sheet2").Range("P2:P3"), _
CopyToRange:=ThisWorkbook.Sheets("Sheet2").Range("A2"), Unique:=True

In the example above the "criteria range" is set to the range P2:P3 within the sheet. Pehaps something like: CriteriaRange:="Depts,1234"

Regards,
John
Regards,
John

User avatar
sdckapr
3StarLounger
Posts: 392
Joined: 25 Jan 2010, 12:21

Re: Advanced Filter Criteria

Post by sdckapr »

Something like this?

Dim rDepts1234 as range
rDepts1234 = ThisWorkbook.Sheets("Sheet2").Range("P2:P3")
oRng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=rDepts1234, _
CopyToRange:=ThisWorkbook.Sheets("Sheet2").Range("A2"), Unique:=True

Steve
Last edited by sdckapr on 15 Mar 2010, 21:38, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 79030
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Advanced Filter Criteria

Post by HansV »

In addition to Steve's reply: the CriteriaRange argument *must* be a reference to a literal range or to a named range, either directly or through a variable as in Steve's example. It cannot be an array.
Best wishes,
Hans