VBA autofilter field using dynamic range

jcooooper

Board Regular
Joined
Mar 24, 2018
Messages
74
Office Version
  1. 365
Platform
  1. Windows
Hello all,

If I have the following line of VBA, can I make the "AutoFilter Field:=7" number dynamic?



Code:
ActiveSheet.Range("Data_column_headers").AutoFilter Field:=7, Criteria1:="="

Ideally I would place my named range in there as my columns move around.


Thanks!
 
Does that range always start in col A?
What is the named range of the column you want to filter?

The range "data_column_headers" will always start in A14 and expand rightwards.

The name of the range I want to filter is "data_portfolio_code". This column is usually in column C, though it may move. I can try to fix this column but would prefer to keep it dynamic if possible.

The criteria I want to filter is if it equals any portfolio code in the named range "data_cash_portfolio_codes"
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
OK, how about
Code:
ActiveSheet.Range("Data_column_headers").AutoFilter Field:=Range("data_portfolio_code").Column, Criteria1:="="
 
Upvote 0
OK, how about
Code:
ActiveSheet.Range("Data_column_headers").AutoFilter Field:=Range("data_portfolio_code").Column, Criteria1:="="

Yeah that works for one filter I am applying. Thanks!

For the other, is it possible to have the following:

Criteria1:="Named_range"

I've tried it but doesn't seem to be working.
 
Upvote 0
Try
Code:
Range("Data_column_headers").AutoFilter Range("data_portfolio_code").Column, Application.Transpose(Range("range1")), xlFilterValues
 
Upvote 0
Try
Code:
Range("Data_column_headers").AutoFilter Range("data_portfolio_code").Column, Application.Transpose(Range("range1")), xlFilterValues

That works perfect and exactly what I was after, thanks so much for persevering.

Just for my own future reference, is there any way I can adapt the formula so that it doesn't equal a named range? I tried ("<>range1") but that didn't work.

Cheers!
John
 
Upvote 0
Nope, unless you are filtering on an exact match you can only use two criteria.
You might want to have a look at advanced filters instead.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,918
Messages
6,175,365
Members
452,638
Latest member
Oluwabukunmi

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