Autofilter multiple criteria in 2003 - more than 2

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
Using Excel 2003.
Is there a limit on the number of criteria that can be used in a VB macro for Autofilter.

I tried to create them in a "for loop" as follows

Code:
Sub Define_Autofilter_Test()
 
    '  assumes that data to be filtered is in column A
    '  test content could be : Prod1, prod2, prod3, prod4 entries
 
    Dim CritArr(10) As String
    numCrit = 3
 
    CritArr(1) = "Prod1"   '
    CritArr(2) = "Prod2"
    CritArr(3) = "Prod3"
 
    crit1 = CritArr(1)
    crit2 = CritArr(2)
 
    For num = 3 To numCrit
        addedCrit = addedCrit & ", Operator:=xlOr, Criteria" & num & ":=" & CritArr(num)
    Next num
 
    Columns("A:A").Select  '  what column to filter
    Selection.AutoFilter Field:=1, Criteria1:=crit1, Operator:=xlOr, Criteria2:=crit2 & addedCrit  ' filter displayed
 
' for msgbox  display   
    OrigCrit = "Field:=1, Criteria1:=" & crit1 & ", Operator:=xlOr, Criteria2:=" & crit2
    NetCrit = OrigCrit & addedCrit
 
'   result is
'   Selection.AutoFilter Field:=1, Criteria1:=Prod1, Operator:=xlOr, Criteria2:=Prod2, Operator:=xlOr, Criteria3:=Prod3
 
    temp = MsgBox("Filter needed by : " & crit1 & " and " & crit2 & " and " & CritArr(3) & vbCr & vbCr & _
                    "Original Criteria : " & OrigCrit & vbCr & vbCr & _
                    "Added   Criteria : " & addedCrit & vbCr & vbCr & _
                    "Final     Criteria : " & NetCrit, vbInformation + vbOKOnly, "Filter results")
End Sub

It only uses filter 1 (Prod1).
It seems to have a problem when there are more than TWO (2) criteris defined

Please comment
 

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.

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
Yes, you can only filter on 2 criteria.

You'll need an alternate method.

I'd suggest a helper column.
Put a formula like =AND(Criteria1,criteria2,criteria3)

Then filter on that column for TRUE.
 

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
jonmo1,

I implemented the "helper" column method that you suggested.

Nicely solved for both 2003 and 2007

Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,129,754
Messages
5,638,170
Members
417,011
Latest member
Amaden95

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