Help with Autofilter and "Field: nn"

jtma67

New Member
Joined
Dec 28, 2003
Messages
1
I have a worksheet that I am using a macro with multiple Autofilter statements to select specific criteria as I create a summary report. One such statement I am using in the macro is:

Code:
Selection.AutoFilter field:=31, Criteria1:="Y", Operator:=xlOr, Criteria2:="N"

My problem is if I add columns anywhere prior to column 31, my macro will return incorrect results...unless of course I appropriately update the "field: =nn" number to correct the offset.

Is there anyway to that I can modify the "field:=" parameter to make it update automatically?

I read an article that discussed a similar situation with VLOOKUP where the column index reference could automatically update.... that code looked like:

Code:
=VLOOKUP(C1,A1:B12,COLUMN(B)-COLUMN(A1)+1)

However I couldn't get the COLUMN function to work. Thanks for any help.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
hi

i was once trapped in a similar situation and i tried something like this.

assumption: row one contains headings.


Sub Button1_Click()

Dim col As Integer

Rows(1).Select
Selection.Find(What:="your Header name of column 31", LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

col = ActiveCell.Column

Selection.AutoFilter Field:=col, Criteria1:="Y", Operator:=xlOr, Criteria2:="N"

End Sub

you may need to modilfy the code to suit your needs but the idea i used was to search for the header name and get the column number returned and then apply the filter on the column number.

Regards
Asim
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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