VBA Custom Filter

MikeDBMan

Well-known Member
Joined
Nov 10, 2010
Messages
608
I am trying to do a filter where either I select records where field 18 equals the variable Ans and field 22 equals Ans4 OR I want to select records where field 30 equals the text "Other"

Code:
                ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=18, Criteria1:="=" & Ans, Operator:=xlAnd, Field:=22, Criteria2:="=" & Ans4, Operator:=xlOr, Field:=30, Criteria1:="Other"

and my error message is wrong number of arguments or invalid property assignment (runtime error 450). Suggesions?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You can only put an operator.

https://docs.microsoft.com/en-us/office/vba/api/excel.range.autofilter



It may be so:

Code:
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=18, Criteria1:=ans
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=22, Criteria1:=ans4
    ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=30, Criteria1:="Other"
 
Last edited:
Upvote 0
That solutions tries to do all 3 as the first criteria AND the second criteria AND the third criteria. But I need it more like this: (Field 18 Criteria=ans AND Field 22 Criteria=ans4) OR Field 30 Criteria="Other". I can't figure how to phrase that.
 
Upvote 0
But with autofilter you can only use one field at a time:

expression. AutoFilter( Field , Criteria1 , Operator , Criteria2 , SubField , VisibleDropDown)

I understand your request, but the way is not the autofilter, maybe you should do the filter using code, for example:

Code:
    Application.ScreenUpdating = False
    ActiveSheet.ListObjects("Table1").DataBodyRange.EntireRow.Hidden = False
    col18 = ActiveSheet.ListObjects("Table1").DataBodyRange.Columns(18).Column
    col22 = ActiveSheet.ListObjects("Table1").DataBodyRange.Columns(22).Column
    col30 = ActiveSheet.ListObjects("Table1").DataBodyRange.Columns(30).Column
    For Each f In ActiveSheet.ListObjects("Table1").DataBodyRange.Rows
        If (Cells(f.Row, col18).Value = ans And Cells(f.Row, col22).Value = ans4) Or _
            Cells(f.Row, col30).Value = "Other" Then
        Else
            Rows(f.Row).EntireRow.Hidden = True
        End If
    Next
    Application.ScreenUpdating = True
 
Upvote 0
Hi,
Why don't you consider adding an additional 'filter' column to which you'll return information whether the record os to be filtered or not based on if the conditions you mentioned are met or not. Having such information in addtional column you can use standard excel filtering method.

Regards,
Sebastian
 
Upvote 0

Forum statistics

Threads
1,215,510
Messages
6,125,241
Members
449,217
Latest member
Trystel

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