VBA - AutoFilter with more than 2 criteria

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,245
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*"
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,245
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,245
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,245
Worked it out. I needed to have a header in my CriteriaRange
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,117
Office Version
  1. 365
Platform
  1. Windows
Glad you sorted it & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,122,915
Messages
5,598,850
Members
414,263
Latest member
sherrcha

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