VBA - filtering columns after inserting or deleting cells

jon1315

Board Regular
Joined
Jun 16, 2020
Messages
60
Office Version
  1. 365
Platform
  1. Windows
Good afternoon

I'm looking for a way to make the field of an autofilter dynamic - for example:

Range("a1:J" & LRow ).autofilter Field:=7, Criteria1:=""

If someone were to add a column between B&C, the field would now need to be 8, but the code won't register this.

I've thought about maybe doing something like:

Dim ColRef as long
ColRef=worksheetfunction.match( [whatever col G header ref is].sheets("sheet1"). Range("A1:J1).0)

But as I'll be putting quite a few filters at various places in the code I'm wondering if there's another way?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Yes, you would need to use a variable for the target column that can locate a string that will constantly be in the target column.
Assume "myHeader" as the string in row 1 of a tartet column
VBA Code:
Dim col As Long
col = Rows(1).Find("myHeader", , xlValues).Column
ActiveSheet.UsedRange.AutoFilter col, Citeria
 
Last edited:
Upvote 0
Thanks for the reply
Would you say that'd be going down the match route?
Thanks
 
Upvote 0
I didn't get the code added quick enough. See my first post.
 
Upvote 0
Having a play with it now - looks to work great! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,785
Messages
6,174,537
Members
452,571
Latest member
MarExcelTips

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