VBA code to apply filter based on multiple selection

somersault

Board Regular
Joined
Jun 24, 2008
Messages
59
Any idea why the below doesnt work in excel 2007??

Original code (as recorded by excel - this works well):
ActiveSheet.Range("$F$4:$G$18").AutoFilter Field:=2, Criteria1:=Array("1", "2", "3"), Operator:=xlFilterValues

Note that 1, 2, 3 are filter selection.

Dyanmic code (to make the filter selection dynamic)
r = sheets("sheet1").range("P2").value
ra = sheets("sheet1").range("P3").value
rb = sheets("sheet1").range("P4").value

ActiveSheet.Range("$F$4:$G$18").AutoFilter Field:=2, Criteria1:=Array(r, ra, rb), Operator:=xlFilterValues

i have defined the filter selections in excel range, and refered to them.

I am unable to find out my mistake, could you help??
 
Hi Guys,

Can Excel 2007 do AutoFilter more than 2 criteria ? or should I used Excel 2010 ? or there's another smart way to cheat with VBA ?

Thank you
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I am sure Mr. Poulsom may help you with a suggestion to use AdvancedFilter if you provide a sample data.
 
Upvote 0
Hello,

Is there any possible to get dynamic filter criteria,like I had the data (Jan, Feb, March, April, May) these are the filter criteria's. If i select the Jan then it will shown the Jan data only. Could please help me with VBA code for above explanation.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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