Hello Hutch,
The Field argument is an offset, going left to right, to the column in the range to be filtered. If you filter only a single column then the field will always be 1. For column "F" to have a Field index of 6, the range would have to include column "A".
Examples
'Single Column
Set Rng = Range("A1:A10")
Rng.AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
Set Rng = Range("F1:F10")
Rng.AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
'Multiple Columns - Filter column "F"
Set Rng = Range("A1:F10")
Rng.AutoFilter Field:=6, Criteria1:="Y", VisibleDropDown:=True
The VisibleDropDown argument either shows (True) or hides (False) the drop down arrow for filtering.
I made a few changes to the macro, mostly to prevent errors. You can set the column you want filter in the range by changing the variable ColIndex.
Sub FilterMacro2()
Dim ColIndex As Long
Dim Rng As Range
Dim RngEnd As Range
'Turn off any Auto Filters
If ActiveSheet.AutoFilterMode Then
Range("1:1").AutoFilter
End If
'Header in row 1
Set Rng = Range("F1")
'Select the column in the range to filter (left to right)
ColIndex = 1
'Set the AutoFilter for the column and the filter value
Rng.Columns(ColIndex).AutoFilter Field:=1, Criteria1:="Y", VisibleDropDown:=True
'Exclude header from filter range
Set Rng = Rng.Columns(ColIndex).Offset(1, 0)
Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))
Addx = Rng.Address
'If they are any filtered cells, delete the entire row for each filtered cell
If Not Intersect(Rng, Rng.SpecialCells(xlCellTypeVisible)) Is Nothing Then
Addx = Rng.SpecialCells(xlCellTypeVisible).Address
Rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
'Display all the data
Rng.Parent.ShowAllData
End Sub
Bookmarks