VB Autofilter Field Criteria

jeefreak

New Member
Joined
Mar 10, 2009
Messages
35
Hi Guys,

Code:
Selection.AutoFilter Field:=9, Criteria1:="Clear"

In the above code, Field 9 refers to the 9th column. In my table each column does have a header name. Column 9's header is "KIND". From time to time this KIND column may move around in the table. Is there a way to filter based on the column header name and not the columns placement?

Code:
Selection.AutoFilter Field:="KIND", Criteria1:="Clear"
I know the above does not work, but is there something similar that's available? Or maybe a way to dynamically index the columns?
 

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.
Hi,

Not elegant but it seems to work

Code:
Sub Test()
    Dim myCol As Long
    
    With Sheet1
        myCol = .Range("Table1[#All]").Find(What:="Kind", After:=.Cells(1, 1), _
            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Column
    End With
    
    Range("Table1[#All]").Select
    Selection.AutoFilter Field:=myCol, Criteria1:="Clear"
End Sub

HTH

M.
 
Upvote 0
Hi,

My previous version had some flaws. Try this

Code:
Sub TestFilter()
    Dim myCol As Long
 
    With Sheet1
        myCol = .Range("Table1[#All]").Find(What:="Kind", _
            After:=.Range("Table1[#All]").Cells(1, 1), LookIn:=xlValues, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Column
 
        myCol = myCol - Range("Table1[#All]").Column + 1
    End With
 
    Range("Table1[#All]").Select
    Selection.AutoFilter Field:=myCol, Criteria1:="Clear"
End Sub

M.
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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