Filter with multiple criteria in VBA

Kerrold

New Member
Joined
Mar 7, 2012
Messages
28
Hi

The following code is quite straight forward and looks for 2 criteria using a filter within some data but I need it to look for more than 2 criteria now as the report is growing...

Selection.AutoFilter Field:=10, Criteria1:="=SERCO", Operator:=xlOr, _
Criteria2:="=NG"

I have tried adding a 3rd criteria based on the code above as show below

Selection.AutoFilter Field:=10, Criteria1:="=SERCO", Operator:=xlOr, _
Criteria2:="=NG", Operator:=xlOr, _ Criteria3:='=NSC"

but it won't work. Any help appreciated

Thank you in advance
Kerry
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello Kerrold,
I have a similar setup, however I use my code compiling multiple sheets into one and only copying data based on my filter so it may need some tweeking but should help.

Code:
Range("A1").Select
    Selection.AutoFilter Field:=10, Criteria1:=Array( _
        "   IL", "   IN", "   MI", "   OH", "   WV"), Operator:=xlFilterValues

I am obviuosly filtering on the 5 states listed, just replace with your criteria.

I also need some help if any knows how to tweek this, in the above AutoFilter instead of hard coded criteria I need to get the criteria from a list in column a of sheet1. Anyone know how to word that?
 
Upvote 0
I also need some help if any knows how to tweek this, in the above AutoFilter instead of hard coded criteria I need to get the criteria from a list in column a of sheet1. Anyone know how to word that?

Say you have a vertucal (column) list named MyList.

Maybe this

Code:
Sub aTest()
    Dim v As Variant
    
    v = Application.Transpose(Range("MyList"))
    Range("A1").Select
    Selection.AutoFilter Field:=1, Criteria1:=v, Operator:=xlFilterValues
End Sub

M.
 
Upvote 0
Unfortunately it didn't work but I found somewhere that multiple criteria doesn't work on excel 2003 only on the more recent version though not sure which ones. Sorted it with a workaround just a bit long winded in comparison. Thank you
 
Upvote 0
I guess the code doesn't work because of the extra _, try this.
Selection.AutoFilter Field:=10, Criteria1:="=SERCO", Operator:=xlOr, _
Criteria2:="=NG", Operator:=xlOr, Criteria3:="=NSC"
 
Upvote 0
Unfortunately it didn't work but I found somewhere that multiple criteria doesn't work on excel 2003 only on the more recent version though not sure which ones. Sorted it with a workaround just a bit long winded in comparison. Thank you

It should work Kerrold, I am using excel 2007, but the worksheet I have the code in is in 2003 format, it was origanally created in 2003. Make sure anywhere you have _ you have a space before and after, and you could take that out if you wanted and make it just one long line instead, the _ is simply to allow you to continue the code on the next line.
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,765
Members
449,121
Latest member
Vamshi 8143

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