ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can I apply Autofilter by Column Title instead of Field #? (https://www.excelbanter.com/excel-programming/390553-can-i-apply-autofilter-column-title-instead-field.html)

MikeZz

Can I apply Autofilter by Column Title instead of Field #?
 
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


Dave Peterson

Can I apply Autofilter by Column Title instead of Field #?
 
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


MikeZz wrote:

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


--

Dave Peterson


All times are GMT +1. The time now is 03:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com