AutoFilter Code not working

kc_native

Board Regular
Joined
Jan 20, 2009
Messages
176
I am trying to delete rows in a data sheet, that don't contain the names that I am specifying in the code below, but I am getting "Compile Error: Named argument not found" when I try to run this part of the code. It is highlighting Criteria3:= when it errors out. Is there a limit to the number of criteria you can use in AutoFilter, or am I just not structuring the code correctly? Thanks for any help you can give me!
Code:
With Range("F11:F5000").AutoFilter(Field:=6, Criteria1:="<>Donahue*", Operator:=xlOr, Criteria2:="<>Edgar*", Operator:=xlOr, _
    Criteria3:="<>Honig*", Operator:=xlOr, Criteria4:="<>McHenry*", Operator:=xlOr, Criteria5:="<>Palmer, Robert", _
    Operator:=xlOr, Criteria6:="<>Stockdale*", Operator:=xlOr, Criteria7:="<>Thatcher*", Operator:=xlOr, _
    Criteria8:="<>Wolfe").SpecialCells(xlCellTypeVisible).EntireRow.Delete(xlUp)
End With
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
AutoFilter can have only 2 criteria, manually or in VBA. Use a formula in a spare column and filter on that. Note that you will need AND not OR for not equals.
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello

This sounds like it calls for an Advanced filter approach ... :)

In any case, Criteria1 and Criteria2 are the only ones you can use for the Autofilter approach. You can filter for the contents of an array, such as for example:

Code:
Sub Autofilter_12a()
     Range("A1").CurrentRegion.AutoFilter _
         Field:=1, _         
         Criteria1:=Array("France", "Belgium", "Germany"), _
         Operator:=xlFilterValues
End Sub
or:
Code:
Sub Autofilter_12b()
     Range("A1").CurrentRegion.AutoFilter _
         Field:=1, _
         Criteria1:=Split("France#Belgium#Germany", "#"), _
         Operator:=xlFilterValues
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,125
Messages
5,599,841
Members
414,342
Latest member
K Darrell Smith

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
Top