Consulting

Results 1 to 8 of 8

Thread: AdvanceFilter- Multiple Criteria in one Cell

  1. #1
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location

    AdvanceFilter- Multiple Criteria in one Cell

    Hello readers,

    I have been working with a company spreadsheet that uses AdvanceFilter method on a range.
    Everything works as expected, but I am not that familiar with how VBA interprets the criteria string.
    Range("Data_Rng").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Range("Criteria"), CopyToRange:=Range("BA15:CR15"), Unique:=False
    I need to be able to, if possible, enter multiple parameters in one cell to apply multiple filters at once.
    For example

    ACOLUMNFilter
    ============
    <>3 AND <> 8


    ACOLUMNDATA
    ===========
    (All rows filtered that don't have 3 or 8 in this column for the dataset)
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Criteria on same row, is And. On multiple rows is multiples Ors. To do the And for one column, duplicate the heading for Criteria. e.g.

    e.g.
    colD      colD
    <>3       <>8

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I always have trouble with the AF so I made a little example to help getting the criteria right. This is a cleaned up version

    Probably a more elegant way to do it

    Capture.JPG

    Option Explicit
    
    
    Sub Macro1()
    
    
        Dim rData As Range, rCrit As Range, rDest As Range
        
        Set rData = Range("A1").CurrentRegion
        Set rCrit = Range("G1").CurrentRegion
        Set rDest = Range("K1")
    
    
        rDest.CurrentRegion.ClearContents
    
    
        Application.CutCopyMode = False
        rData.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=rDest, Unique:=False
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    I need to be able to, if possible, enter multiple parameters in one cell to apply multiple filters at once.
    You can use formula as criteria.


    =and(A2<>3,A2<>8)

  5. #5
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    I tried adding a second column with same heading and both filters stopped working.
    This is the code the previous colleague had developed.

    CritRows = Range("A1").CurrentRegion.Rows.Count
        If CritRows = 1 Then
            ActiveWorkbook.Names.Add Name:="Criteria", RefersTo:=Range("A1:AR2")
        Else
            Range("A1").CurrentRegion.Select
            ActiveWorkbook.Names.Add Name:="Criteria", RefersTo:=Selection
        End If
        
        Range("A15").CurrentRegion.Select
        ActiveWorkbook.Names.Add Name:="Data_Rng", RefersTo:=Selection
        
        Range("Data_Rng").AdvancedFilter Action:=xlFilterCopy, _
        CriteriaRange:=Range("Criteria"), CopyToRange:=Range("BA15:CR15"), Unique:=False
        
        Range("A15").Select
    mana
    You can use formula as..

    =and(A2<>3,A2<>8)

    That had no affect either.


    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Maybe it is time to make a simple example file and post it?

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    No, it's high time.

  8. #8
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    I stand corrected. Perhaps I was just mistyping the headers. It works with a second header column in the filter range.
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

Tags for this Thread

Posting Permissions

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