VBA - AutoFilter with more than 2 criteria

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,251
I would like to autofilter with 5+ criteria, but my code falls over if I try more than 2.

is there a work around or alternative method without stepping through each line?

Works
Code:
Sheets("data").Range("A:P").AutoFilter Field:=1, Criteria1:="<>R2*" _
        , Operator:=xlAnd, Criteria2:="<>T2*"

Doesn't Work
Code:
Sheets("data").Range("A:P").AutoFilter Field:=1, Criteria1:="<>R2*" _
        , Operator:=xlAnd, Criteria2:="<>T2*" _
        , Operator:=xlAnd, Criteria3:="<>N2*" _
        , Operator:=xlAnd, Criteria4:="<>B2*" _
        , Operator:=xlAnd, Criteria5:="<>DEV*"
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,251
Thanks for that, looks like advancedfilter will replace a significant part of my code, but I can't quite see where the actual criteria is specified, or how to add multiple criteria

Code:
     Sheets("data").Range("A:P").AdvancedFilter _        Action:=xlFilterCopy, _
        CriteriaRange:=Sheets("data").Range("A:A") := "<>R2*", _
        CriteriaRange:=Sheets("data").Range("A:A") := "<>T2*", _
        CriteriaRange:=Sheets("data").Range("A:A") := "<>B2*", _
        CriteriaRange:=Sheets("data").Range("A:A") := "<>DEV*", _
        copytorange:=Sheets("CTB").Range("A1") ', _ ??
       ' Unique:=True ??
 
Last edited:

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,251
Fluff, I am sorry but I cannot get my head around why this doesn't work.

I have simplified the criteria just for testing and the advance filter doesn't give any error, however it returns all data.


Code:
Sheets("data").Range("A:P").AdvancedFilter _
        Action:=xlFilterinplace, _
        CriteriaRange:= Sheets("Filter_Data").Range("A1")


The value in Sheets("Filter_Data").Range("A1") is T2*
 

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,251
Worked it out. I needed to have a header in my CriteriaRange
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,038
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,561
Messages
5,832,512
Members
430,138
Latest member
silver_john

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top