+ Reply to Thread
Results 1 to 8 of 8

Filter by multiple criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-27-2012
    Location
    Somerset, Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    123

    Filter by multiple criteria

    See attached file.

    I’d like to add VBA to automatically filter a table (see attached example).
    I want to filter and keep everything in the table where the value in Column D equals Blue, Black, or Red.
    I also want the filter to then keep anything in Column B where the value is equal to any text string or is less than or equal to the date in cell K1.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Filter by multiple criteria

    I don't think AutoFilter can do...
    AdvancedFilter.
    Sub test()
        Dim r As Range
        With Sheets("sheet1").Cells(1).CurrentRegion
            Set r = .Offset(, .Columns.Count + 1).Range("a1:a2")
            r(2).Formula = "=or(b2=k$1,or(d2={""blue"",""black"",""red""}))"
            .AdvancedFilter 1, r
            r(2) = Empty
        End With
    End Sub

  3. #3
    Forum Contributor
    Join Date
    07-27-2012
    Location
    Somerset, Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    123

    Re: Filter by multiple criteria

    I tested it. Thanks, by the way. It seemed to do fine for Column D, but it didn't do the next filter on Column B

  4. #4
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,223

    Re: Filter by multiple criteria

    Please try

    Sub filt()
    With Cells(1).CurrentRegion
        .AutoFilter
        .AutoFilter Field:=4, Criteria1:=Array("Blue", "Black", "Red"), Operator:=xlFilterValues
        .AutoFilter Field:=2, Criteria1:="=*", Operator:=xlOr, Criteria2:="<=" & [K1]
    End With
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Filter by multiple criteria

    Quote Originally Posted by scrabtree23 View Post
    I also want the filter to then keep anything in Column B where the value is equal to any text string or is less than or equal to the date in cell K1.
    Missed
    Sub test()
        Dim r As Range
        With Sheets("sheet1").Cells(1).CurrentRegion
            Set r = .Offset(, .Columns.Count + 1).Range("a1:a2")
            r(2).Formula = "=or(and(b2<>"""",or(istext(b2),b2<=k$1)),or(d2={""blue"",""black"",""red""}))"
            .AdvancedFilter 1, r
            r(2) = Empty
        End With
    End Sub
    Last edited by jindon; 01-07-2021 at 10:02 AM. Reason: Fixed the formula to exclude blank.

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,027

    Re: Filter by multiple criteria

    Sub sintekJ3v16()
    With Cells(1).CurrentRegion
        .AutoFilter 2, "*", xlOr, "<=" & CDbl(Range("K1")) & ""
        .AutoFilter 4, Array("Blue", "Black", "Red"), xlFilterValues
    End With
    End Sub
    Last edited by Sintek; 01-07-2021 at 10:49 AM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  7. #7
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,223

    Re: Filter by multiple criteria

    @sintek
    Thanks for the CDbl tip

    I'm quite a newbie for VBA.
    Why do we convert the Date to double?

    To prevent m/d/y d/m/y conflict ?
    Last edited by Bo_Ry; 01-07-2021 at 11:03 AM.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,027

    Re: Filter by multiple criteria

    Dates are stored as numbers...Cdbl() changes to double for filter conversion...
    Can make use of Clng() or Cdate() in some cases...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Filter with multiple criteria
    By edesroch in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-28-2020, 03:53 PM
  2. Multiple criteria filter of multiple tables with macro
    By reese_c_c in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-07-2018, 09:56 AM
  3. Replies: 0
    Last Post: 08-04-2017, 02:39 AM
  4. Filter Excel Data using vba multiple criteria multiple columns
    By pmyk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2013, 01:32 AM
  5. Need to filter multiple criteria in Label Filter
    By brassellc7994 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 04-04-2013, 12:54 PM
  6. Replies: 2
    Last Post: 06-15-2012, 05:34 PM
  7. Multiple Criteria Filter
    By gangulyatanu in forum Excel General
    Replies: 1
    Last Post: 02-07-2012, 03:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1