Autofilter with more than 2 criteria

BenNV

Board Regular
Joined
Mar 27, 2002
Messages
160
Thanks for the help earlier, Northerner.

Wondering if anyone can help on this one:

Selection.AutoFilter Field:=1, Criteria1:=lowDate, Operator:=xlOr, _
Criteria2:=birthDate, Operator:=xlOr, Criteria3:=highDate

The above does not work because of more than two criteria. What can I do.?
 

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 Ben,

As you have discovered, AutoFilter will only accept two criteria for any one filter operation. Depending on how many criteria you have it is possible to use a 'workaround' like this:
Code:
Sub FilterCopy() 
Dim rngTarget As Range, rng1 As Range, rng2 As Range, rngMyRange As Range 
Const Crit1 As String = "Crit1" 
Const Crit2 As String = "Crit2" 
Const Crit3 As String = "Crit3" 

Application.ScreenUpdating = False 
With Worksheets("Sheet1") 
    Rows(1).Insert 
    .Range("A1").Value = "dummy" 
    'set up dummy header 
    Set rngTarget = .Range("A1:A" & .Cells(Rows.Count, 1).End(xlUp).Row) 
    'set the range to work with 
    rngTarget.AutoFilter Field:=1, Criteria1:=Crit1, Operator:=xlOr, Criteria2:=Crit2 
    'get first two criteria 
    Rows(1).Hidden = True 
    'hide dummy header 
    Set rng1 = rngTarget.SpecialCells(xlCellTypeVisible) 
    'range with first 2 
    rngTarget.AutoFilter 
    'reset autofilter 
    rngTarget.AutoFilter Field:=1, Criteria1:=Crit3 
    'get third criteria 
    Rows(1).Hidden = True 
    'hide dummy header 
    Set rng2 = rngTarget.SpecialCells(xlCellTypeVisible) 
    'range with 3rd 
    rngTarget.AutoFilter 
    Set rngMyRange = Union(rng1, rng2) 
    'combine the range 
    rngMyRange.EntireRow.Copy _ 
        Destination:=Worksheets("Sheet2").Range("A65536").End(xlUp).Offset(1, 0) 
    'or whatever you want to do ... 
End With 
Application.ScreenUpdating = True 

End Sub
However, just because you can do it doesn't mean you should :wink: What is your objective? Have you tried using the AdvancedFilter?
 
Upvote 0
Thanks Ritchie.

My objective is for the user to enter a date (birthDate) and the program to return the day before(lowDate) and day after (highDate)

Selection.AutoFilter Field:=1, Criteria1:=lowDate, Operator:=xlOr, _
Criteria2:=birthDate, Operator:=xlOr, Criteria3:=highDate
 
Upvote 0
Ritchie:

I could use the following:

Selection.AutoFilter Field:=7, Criteria1:=>=lowDate, Operator:=xlAnd _
, Criteria2:=<=highDate

But I get an error, where could I put the " "?
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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