AutoFilter Field # - How do numeric designations work?

hip2b2

Board Regular
Joined
May 5, 2003
Messages
135
Office Version
  1. 2019
Platform
  1. Windows
I am using the following code, works just fine. My question has to do with the field designation.

VBA Code:
 .AutoFilter Field:=11, Criteria1:="=Closed" & strName & "*", Operator:=xlAnd
 .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
 .AutoFilter Field:=11

The field that contains the filter criteria is Col S. As S is not the 11th letter of the alphabet how does this work? I do note that Col S is the 11th Col in my spreadsheet that is populated with text, or am I drawing a false conclusion?

Is there any simple way to modify the code to relate to an absolute column designation?

Thanks

hip
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
It is the column index, where the first filtered column is 1. So 11 is the 11th column in the autofilter, which suggests your data starts in col I
 
Upvote 0
It is the column index, where the first filtered column is 1. So 11 is the 11th column in the autofilter, which suggests your data starts in col I

Well that makes more sense that what I was supposing.

While there is data in Col I it is definitly not the data that is being filtered.
 
Upvote 0
While there is data in Col I it is definitly not the data that is being filtered.
No but it sounds as though col I is the 1st column in the auto filter range.
 
Upvote 0
Ahah!!!???? So to do what I should have done, here is the rest of the code:

VBA Code:
Set h = Sheets("For XYZ")
If h.AutoFilterMode Then h.AutoFilterMode = False
lr = Columns("A:K").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With h.Range("A6:K" & lr)

.AutoFilter Field:=2, Criteria1:="=Open" & strName & "*", Operator:=xlAnd
.Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
.AutoFilter Field:=2

.AutoFilter Field:=11, Criteria1:="=Closed" & strName & "*", Operator:=xlAnd
.Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
.AutoFilter Field:=11

Not sure how to set the range properly

Thanks again
 
Last edited:
Upvote 0
You need to qualify the line getting the last row with the sheet like
VBA Code:
lr = h.Columns("A:K").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
Upvote 0
Not quite sure what to do with this. I tried to change the range from A:K to A:S and the Field from 11 to 19 but the macro fails at that point.
 
Upvote 0
What is the range you want to filter & which columns?
 
Upvote 0
Two ranges actually.
  • One Range is Col S
  • One Range is Col B
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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