Can I apply Autofilter by Column Title instead of Field #?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following code that works:
Selection.AutoFilter Field:=3, Criteria1:="GMT001(2)"

I want to be able to do something like:
Selection.AutoFilter Field:="Car Model", Criteria1:="GMT001(2)"

Eventually the "Car Model" would be assigned a variable of Text Type.

Any suggestions on how I could do this?
If there's no silver bullet, is there a property or something that I could
latch on to that has the list of Index'd Column Titles or at least the far
left cell address to offset from?

Thanks,

MikeZz
 
The autofilter is already applied to the correct range?

If yes:

Option Explicit
Sub testme01()

Dim myCell As Range
Dim res As Variant
Dim myTitle As String
Dim myCrit As String

myTitle = "Car Model"
myCrit = "gmt001(2)"

With Worksheets("sheet1").AutoFilter.Range
res = Application.Match(myTitle, .Rows(1), 0)
If IsError(res) Then
'no cell with that value
'do nothing
Else
.AutoFilter field:=res, Criteria1:=myCrit
End If
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top