Filter macro

Alvaroro84

Board Regular
Joined
May 13, 2022
Messages
65
Office Version
  1. 2016
Platform
  1. Windows
Is it possible to create a macro that filters by using the headers name rather then the specific column? If so what would something like that look like?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You should find column index by exact match and put it to variable and then use that index instead column name.
Example:
VBA Code:
Sub FileterByHeader()

    vCHeader = Rows(1).Find("Header 2", , xlValues, xlWhole, , , True).Column
    Range("A1:E5").AutoFilter vCHeader, "Some Text"
    
End Sub
 
Upvote 0
Solution
If the data happens to be in a formal Excel table (ListObject) then you could try something like this

VBA Code:
Sub Fltr()
  With ActiveSheet.ListObjects(1)
    .Range.AutoFilter Field:=.ListColumns("Header 2").Index, Criteria1:="Some Text"
  End With
End Sub
 
Upvote 0
About my example.
If you are interested about look, using the match function code might look like this too.
VBA Code:
    With ActiveSheet.Range("A1:E5")
        .AutoFilter Application.Match("Header 2", .Rows(1), 0), "Some Text"
    End With
 
Upvote 0
Thank you for the reply i was wondering if it was possible to add columns based off a columns name
 
Upvote 0
That sounds like a very different question. I suggest that you start a new thread for it.
 
Upvote 0

Forum statistics

Threads
1,216,574
Messages
6,131,495
Members
449,653
Latest member
aurelius33

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