Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA AutoFilter with array Criteria

    The AutoFilter in the snip below does not select any rows. arArray is defined by Dim arArray() as Long. The previous step loads the array (1) to (3) with three valid long values (checked by locals view.) If I remove the Operator:=xlFilterValues the row with the matching value for (3) is selected as described for AutoFilter. This project has other single criteria AutoFilters working fine but with strings

    Help me see what I'm doing wrong. Using Office 2010 32 bit version.

    Code:
    '----------- Step 3: Autofilter to display items matching the Bar's in the array.
    
    Worksheets("Sheet1").Activate
    Sheet1.Range(sRng).AutoFilter
    Sheet1.UsedRange.AutoFilter Field:=iBar, Criteria1:=arArray, Operator:=xlFilterValues
    Sheet1.Cells.Sort Key1:=Range(sAut), key2:=Range(sSrt), Order1:=xlAscending, _
        Header:=xlYes, MatchCase:=False, Orientation:=xlSortColumns
    Display:
    Thanks, Buford

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    You need to declare the array as String even if the data is numeric.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    New Lounger
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks a lot. Works OK.

    Buford

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 Posts
    Quote Originally Posted by rory View Post
    You need to declare the array as String even if the data is numeric.
    Ok Rory, you got me confused. Could you please elaborate...inquiring minds want to know...and so do I!
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Sure.

    Even if you want to filter for say the values 1, 2, 3 and 4 the array you pass to the autofilter criteria actually needs to contain "1", "2", "3" and "4". The simple way to do that is to declare your array variable as a String rather than Numeric type so that whatever you populate it with ends up as text.
    Regards,
    Rory

    Microsoft MVP - Excel

  6. The Following User Says Thank You to rory For This Useful Post:

    RetiredGeek (2013-05-20)

Posting Permissions

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