AutoFilter with dynamic range criteria

AlexCS

Board Regular
Joined
Oct 31, 2011
Messages
78
Hi everyone,

Can anyone tell me if it is even possible to use a dynamic range as an autofilter criteria? I tried with the below code and it does not work..a close variation also gets my Excel to crash

Dim var As Integer
Sheets("Master").Select
ActiveSheet.Cells(1, 1).CurrentRegion.Select
var = Selection.Rows.Count

Dim sir As Range
Set sir = Sheets("Master").Range(Cells(2, 1), Cells(var - 1, 1))
Sheets("Total Despatched").Select
ActiveSheet.Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$DI$2500").AutoFilter Field:=94, CriteriaRange:=sir, Operator:=xlFilterValues

What I am trying to do is identify the range in the Master tab and then using it to filter the Total Despatched tab for each entry in the range.

Any help would be much appreciated!

Thanks,

Alex
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The filtervalues need to be passed as a 1 dimensional array, so assuming it is a verticla single column range you could use:

Code:
ActiveSheet.Range("$A$1:$DI$2500").AutoFilter Field:=94, CriteriaRange:=Application.Transpose(sir), Operator:=xlFilterValues
 
Upvote 0
Good morning Firefly,

Thank you very much for your suggestion. Couldn't wait to get to work and try this but unfortunately, when trying to run the modified macro, I got Run time error 1004 - Application Defined or Object Defined Error.

I also tried to define another range to take the value of Application.Transpose(sir) and filter by it but I got Run time error 424 Object Required.

I will look at the exact syntax for using worksheet applications in VBA code but other than this I am out of ideas..is there anything else you would try?

Best,

Alex
 
Upvote 0
My apologies - you have an invalid argument in the Autofilter. It should be:

ActiveSheet.Range("$A$1:$DI$2500").AutoFilter Field:=94, Criteria1:=Application.Transpose(sir), Operator:=xlFilterValues
 
Upvote 0
Hi Firefly,

Sorry, tried to check my posts but there were some issues with the forum. I just tested your solution and it works perfectly! I am really happy to have learned something that can prove to be very useful in the future :)

Thanks a lot for your time!

Alex
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,904
Members
449,194
Latest member
JayEggleton

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