VBA - AutoFilter with more than 2 criteria

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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:
Upvote 0
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*
 
Upvote 0
Worked it out. I needed to have a header in my CriteriaRange
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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
Back
Top