+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    03-20-2010
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    7

    More than 2 criterias in an autofilter

    Hi there!

    I've all morning trying to resolve this so please help me...

    All I need is just to have 3 or more different criterias in an autofilter.

    I tried this but it wont work...

    please give a hand!

    Thanks!

    Code:
        Dim a, b, c, a1, b1, c1 As String
    
        a = Cells(5, 17).Value
        b = Cells(6, 17).Value
        c = Cells(7, 17).Value
    
        a1 = Cells(5, 18).Value
        b1 = Cells(6, 18).Value
        c1 = Cells(7, 18).Value
    
        ....
    
        ActiveSheet.Range("$A$4:$N$15132").AutoFilter Field:=1, Criteria1:=a, Operator:=xlOr, Criteria2:=b, Operator:=xlOr, Criteria3:=c
    
        ActiveSheet.Range("$A$4:$N$15132").AutoFilter Field:=3, Criteria1:=a1, Operator:=xlOr, Criteria2:=b1, Operator:=xlOr, Criteria3:=c1
    Last edited by royUK; 03-20-2010 at 11:51 AM. Reason: add code tags

  2. #2
    Forums Administrator royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    24,447

    Re: More than 2 criterias in an autofilter

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    Code Tags added this time
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel consulting, free examples and tutorials visit Excel Consulting-Excel VBA
    Check out the free Excel Toolbar

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)


    Code Tags: Make your code easier for us to read

  3. #3
    Registered User
    Join Date
    03-20-2010
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    7

    Autofilter criteria

    Please tell why this works


    Code:
        ActiveSheet.Range("$A$4:$N$15131").AutoFilter Field:=1, Criteria1:=Array("154", "155", "156", "157"), Operator:=xlFilterValues
    and this doesn't

    Code:
    Dim a, b, c, d As String
    
    a = 154
    b = 155
    c = 156
    d = 157
    
    
        ActiveSheet.Range("$A$4:$N$15131").AutoFilter Field:=1, Criteria1:=Array(a, b, c, d), Operator:=xlFilterValues

  4. #4
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    2,815

    Re: Autofilter criteria

    Your dim statement has only declared variable d as a string. In the absence of a variable type, the variant type is used by default. This means that a, b and c behave like integers.

    Try using

    Code:
    Dim a As String, b As String, c As String, d As String
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  5. #5
    Forum Guru Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007
    Posts
    3,441

    Re: Autofilter criteria

    Excel versions prior to 2007 only support two criteria and arrays, as far as I know, cannot be used within Auto-Filter. Don't have Excel 2007 on this PC, so, not sure if it accepts use of arrays - but it does support many more criteria than prior versions.

    If the array approach does not work, it would be quite simple and just as efficient to pass the string variables into a range of cells and specify that range as criteria for Advanced Filter.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  6. #6
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Autofilter criteria

    hi Machado,

    Welcome to the forum

    I think you should have put this post in your previous thread because it seems to be the same issue to me. Hopefully a Mod will spot this & they can merge the two threads...

    Edit: My connection's playing up today (again!) so I'm sorry about the double up of info - see Martin's previous post for the same comments about your variable declarations. /end edit.

    I couldn't see any relevant* information in either the VBE or Excel's 2007 Help files for Autofilter but perhaps the following may help...
    *Unless, do you have a mixture of formats in the column to be filtered (eg string & number)?

    Code:
    Dim a, b, c, d As String
    'the above line of code is the equivalent of the below
    Dim a as variant, b as variant, c as variant, d As String
    'when I think your intent is more along the lines of...
    Dim a As String, b As String, c As String, d As String
    If you need more than two criteria, as you've mentioned in the previous thread, then I suggest you use a Helper column containing a formula which returns a True or False (perhaps an If statement) if any of the criteria are fulfilled.

    hth
    Rob
    Last edited by broro183; 03-20-2010 at 02:07 PM.
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  7. #7
    Registered User
    Join Date
    03-20-2010
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: More than 2 criterias in an autofilter

    Thanks!!!!!

Thread Information

Users Browsing this Thread

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

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.2.0