Can I name an AutoFilter Field?

DUI

Board Regular
Joined
Jul 3, 2006
Messages
109
Hi Guys,

Rather than using:
Code:
    Selection.AutoFilter Field:=24, Criteria1:="2"
is there a way I can use the heading name of that column instead of its' Field number..?
Every time I insert or move a column I have to change all these numbers...

Thanks in advance.

John.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Yep, the likes of:
Code:
Selection.AutoFilter Field:=Application.Match("Header 2", Selection.Parent.AutoFilter.Range.Rows(1)), Criteria1:="2"
with an error message:
Code:
Sub test()
ColHeadr = "Header 2"
Set xxx = Selection.Parent.AutoFilter.Range.Rows(1)
Set yyy = xxx.Find(ColHeadr)
If Not yyy Is Nothing Then
  Selection.AutoFilter Field:=Application.Match(ColHeadr, xxx), Criteria1:="2"
Else
  MsgBox ColHeadr & " doesn't exist in the autofilter"
End If
End Sub
I've stuck with 'selection' following your example, but you could tweak it to use a sheet-qualified range instead, that way the sheet wouldn't need to be the active sheet and no selection would be necessary.

Hi Guys,

Rather than using:
Code:
    Selection.AutoFilter Field:=24, Criteria1:="2"
is there a way I can use the heading name of that column instead of its' Field number..?
Every time I insert or move a column I have to change all these numbers...

Thanks in advance.

John.
 
Upvote 0
Thanks for your help p45cal,

I also got this working:
Code:
Sub testname()


Dim rng As Range, res As Variant
Set rng = Range("C11:X11").Cells
res = Application.Match("Header1", rng, 0)
If Not IsError(res) Then
Selection.AutoFilter Field:=res, Criteria1:="2"
Else
MsgBox "Header1 was not found"
End If

End Sub

Not sure if one is better than the other...

Regards,

John.
 
Upvote 0
Your error trapping is more succinct than mine, but your setting is hard coded, so you'll still have the problem of changing the code when you add columns.
The autofilter object sits under the sheet object (since there's only one per sheet, no problem there), so however big or wherever the autofilter is on the sheet the code won't need changing if you use just the sheet to identify the autofilter range.
Code:
Sub test2()
Dim rng As Range, res As Variant
Set rng = ActiveSheet.AutoFilter.Range.Rows(1)  'qualify the sheet any way you want, can be any sheet, no selection needed.
res = Application.Match("Header1", rng, 0)
If Not IsError(res) Then
  rng.AutoFilter Field:=res, Criteria1:="2"
Else
  MsgBox "Header1 was not found"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,645
Latest member
Tante

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