+ Reply to Thread
Results 1 to 13 of 13

Use multiple cell values in autofilter criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    07-08-2016
    Location
    Prague
    MS-Off Ver
    2010
    Posts
    15

    Use multiple cell values in autofilter criteria

    Good Morning!

    I'm trying to make a macro filter, but I don't know how to use multiple cells as criteria.

    I'm using this:

    Range(Selection, Selection.SpecialCells(xlLastCell)).AutoFilter Field:=10, Criteria1:=Sheets("Sheet4").Range("C3:K3")
    But the filter is using only the value in K3.

    Can anyone help me please?

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,028

    Re: Use multiple cell values in autofilter criteria

    Plese use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting and make different than other text.

    Highlight your code and click the # icon at the top of your post window.

    I've fix it now for you.
    Never use Merged Cells in Excel

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,028

    Re: Use multiple cell values in autofilter criteria

    Try this:

    Sub FilterRange()
        Dim N As Long, r As Range
    
    With Sheets("Sheet4")
        .AutoFilterMode = False
          
         N = 9 ' Number of columns in range
            ReDim ary(1 To N)
            For i = 3 To N+2 ' for columns C to K
                ary(i) = .Cells(3, i) ' 3 is row number, i is column number
            Next i
    End with
    
    With Activesheet.Range(Selection, Selection.SpecialCells(xlLastCell))
    
        .AutoFilter
        .AutoFilter Field:=10, Criteria1:=ary, Operator:=xlFilterValues
    End With
    
    End Sub

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: Use multiple cell values in autofilter criteria

    Or this.
    Range(Selection, Selection.SpecialCells(xlLastCell)).AutoFilter 10, _
        Filter([if(Sheet4!C3:K3)="","~",Sheet4!C3:K3)], "~", False), xlFilterValues
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Registered User
    Join Date
    07-08-2016
    Location
    Prague
    MS-Off Ver
    2010
    Posts
    15

    Re: Use multiple cell values in autofilter criteria

    Thanks for your help zbor and bakerman2!

    I forgot to tell this:
    The cells I want to use as criteria are in Sheet4, and the table is in Sheet3.

    Let me show you all the code:
    Sub FilterRange()
        Sheets("Sheet3").Select
        Range("A1").Select
        Range(Selection, Selection.SpecialCells(xlLastCell)).Select
        Selection.AutoFilter
        Range(Selection, Selection.SpecialCells(xlLastCell)).AutoFilter Field:=3, Criteria1:=Sheets("Sheet5").Range("A4")
        Range(Selection, Selection.SpecialCells(xlLastCell)).AutoFilter Field:=4, Criteria1:= _
            "~*~*~*~*"
        Range(Selection, Selection.SpecialCells(xlLastCell)).AutoFilter Field:=10, Criteria1:=Sheets("Sheet4").Range("C3:K3")
    End Sub
    I don't know how to mix your code with mine...

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: Use multiple cell values in autofilter criteria

    Maybe something like this.
    Sub FilterRange()
        With Sheets("Sheet3")
            .Cells(1).CurrentRegion.AutoFilter 3, [Sheet5!A4]
            .Cells(1).CurrentRegion.AutoFilter 4, "~*~*~*~*"
            .Cells(1).CurrentRegion.AutoFilter 10, _
                Filter([if(Sheet4!C3:K3="","~",Sheet4!C3:K3)], "~", False), xlFilterValues
        End With
    End Sub
    Last edited by bakerman2; 08-30-2016 at 02:10 AM. Reason: error in formula

  7. #7
    Registered User
    Join Date
    07-08-2016
    Location
    Prague
    MS-Off Ver
    2010
    Posts
    15

    Re: Use multiple cell values in autofilter criteria

    Dank u wel / merci beaucoup bakerman2!

    I placed your macro but it shows me this error:
    "Type mismatch"

    Any idea?

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: Use multiple cell values in autofilter criteria

    Hard to say without seeing example file to test on.

  9. #9
    Registered User
    Join Date
    07-08-2016
    Location
    Prague
    MS-Off Ver
    2010
    Posts
    15

    Re: Use multiple cell values in autofilter criteria

    Hello Bakerman2

    I've upload a very simple exemple to make you know what I want.

    The result I am searching for is the one you can see in Sheet2

    Thanks in advance for your help!
    Attached Files Attached Files

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: Use multiple cell values in autofilter criteria

    Sorry for late response.
    This works in your example file.
    Sub FilterRange2()
        With Sheets("Sheet2").Cells(1).CurrentRegion
            .AutoFilter 3, [Sheet3!A5]
            .AutoFilter 4, Filter([if(Sheet3!B5:C5="","~",Sheet3!B5:C5)], "~", False), xlFilterValues
        End With
    End Sub

  11. #11
    Registered User
    Join Date
    07-08-2016
    Location
    Prague
    MS-Off Ver
    2010
    Posts
    15

    Re: Use multiple cell values in autofilter criteria

    Hello Bakerman2!

    Thanks for your help!
    I need to replace your code with mine and see how can it work.

  12. #12
    Registered User
    Join Date
    07-08-2016
    Location
    Prague
    MS-Off Ver
    2010
    Posts
    15

    Re: Use multiple cell values in autofilter criteria

    Hello Bakerman2!

    I replaced correctly your code and it works perfectly!

    Thank you so much!

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: Use multiple cell values in autofilter criteria

    You're welcome.

+ 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] How to return multiple values to a single cell based on multiple criteria
    By lwallace in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-20-2014, 06:32 AM
  2. Autofilter multiple columns with multiple string criteria
    By svalentine91 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-23-2013, 11:26 AM
  3. VBA Autofilter multiple criteria
    By Daduka in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-29-2013, 08:36 AM
  4. Aggregate multiple values into one cell based on multiple criteria using VBA
    By jwestover1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-04-2013, 01:17 PM
  5. [SOLVED] Autofilter with multiple criteria - how to ignore blank criteria
    By MOONDRIFT47 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-31-2012, 12:20 PM
  6. Multiple Criteria for Autofilter using And & Or
    By Wongadob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2011, 01:39 PM
  7. multiple column , multiple criteria autofilter sort
    By jseufert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2009, 06:18 PM

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