RE: VBA filter by 3rd criteria

philhowell

Board Regular
Joined
Jun 11, 2002
Messages
175
RE: VBA filter by 3rd criteria

How can i filter three criteria's.........

this does two....... :D
Selection.AutoFilter Field:=6, _
Criteria1:="<>A*", Operator:=xlAnd, _
Criteria2:="<>W*", Operator:=xlAnd

but this doesn't work........ :(
Selection.AutoFilter Field:=6, _
Criteria1:="<>A*", Operator:=xlAnd, _
Criteria2:="<>W*", Operator:=xlAnd
Criteria2:="<>Q*"
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You have Criteria2 listed twice. Is this a typo on your post or in your code?
 
Upvote 0
Hi Phil,

The AutoFilter will only accept two criteria.

As a workaround, perform your first filter with 2 criteria and set a range variable equal to the visible cells. Then show all data and filter again with the third criteria and set another range variable to these visible cells. Then define your final range as the union of these other two ranges.

HTH
 
Upvote 0
Sorry, criteria2 used twice was a typo (i jus' copied and pasted the second line)........ changing it doen't help tho :cry:

Richie, can you give me an example snippet of code........

cheers
phil
 
Upvote 0
Hi Phil,

Something 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
HTH
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,028
Members
449,092
Latest member
ikke

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