+ Reply to Thread
Results 1 to 9 of 9

Autofilter using Array & loop

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Autofilter using Array & loop

    Hi,

    can anyone help me so that it works.

    Dim rng As Range
    Dim i, lng As Long
    Dim sCriteria As Variant
    
                i = Range("G" & Rows.Count).End(xlUp).Row
          Set rng = Range("A12:TB" & i)
        sCriteria = Array("BC, HEA*", "Deputy Hea*", "Head, Bi*")
        
        
        For lng = LBound(sCriteria) To UBound(sCriteria)
           With rng
                .AutoFilter
                .AutoFilter Field:=2, Criteria1:="Business Banking"
                .AutoFilter Field:=6, Criteria1:=sCriteria(lng)
            End With
        Next lng

  2. #2
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Autofilter using Array & loop

    try this....

    Dim rng As Range
    Dim i, lng As Long
    Dim sCriteria As Variant
    
                i = Range("G" & Rows.Count).End(xlUp).Row
          Set rng = Range("A12:TB" & i)
        sCriteria = Array("BC, HEA*", "Deputy, Hea*", "Head, Bi*")
        
        
        For lng = LBound(sCriteria) To UBound(sCriteria)
           With rng
                .AutoFilter
                .AutoFilter Field:=2, Criteria1:="Business Banking"
                .AutoFilter Field:=6, Criteria1:=sCriteria(lng)
            End With
        Next lng
    Don't forget to click *

  3. #3
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: Autofilter using Array & loop

    hi vikas,

    There is no difference with my original post. at which point you make correction?,

    I want the filter to display all sCriteria.

  4. #4
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Autofilter using Array & loop

    Farid,

    When you're looping, don't you want to copy the results of the each loop?

    Secondly, consider using the XlAutoFilterOperator, unless you're looking for values which meet both criteria, for instance (for loop 1) both "Business Banking" (in column 2) and "BC, HEA*" (in column 6).
    Please * if you like the answer

  5. #5
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: Autofilter using Array & loop

    Hi GIS2013,

    how to copy the result of each loop?

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Autofilter using Array & loop

    Hi Faridwahidi,
    maybe something like this
    Sub ertert()
    Dim r As Range, adr As String, sCriteria As Variant, v, f$
    sCriteria = Array("BC, HEA", "Deputy Hea", "Head, Bi"): f = "~"
    With Range("A12:TB" & Cells(Rows.Count, "G").End(xlUp).Row)
        With .Columns(6)
            For Each v In sCriteria
                Set r = .Find(What:=v)
                If Not r Is Nothing Then
                    adr = r.Address
                    Do
                        If InStr(f, "~" & r.Value & "~") = 0 Then f = f & r.Value & "~"
                        Set r = .FindNext(r)
                    Loop While r.Address <> adr
                End If
            Next v
        End With
            .AutoFilter
            .AutoFilter 2, "Business Banking"
            .AutoFilter 6, Split(Mid(f, 2), "~"), 7
    End With
    End Sub

  7. #7
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Autofilter using Array & loop

    I'm sure all the solutions you've got here are good, I tried to remain with your code structure.

    Check the file, you'll to make your adjustments.
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: Autofilter using Array & loop

    Hi nilem,

    Thanks a lot. Your code working fine. for me, it is quite tough to understand this part. if possible, can you explain to me?

    For Each v In sCriteria
                Set r = .Find(What:=v)
                If Not r Is Nothing Then
                    adr = r.Address
                    Do
                        If InStr(f, "~" & r.Value & "~") = 0 Then f = f & r.Value & "~"
                        Set r = .FindNext(r)
                    Loop While r.Address <> adr
                End If
            Next v





    GIS2013 ,
    Your code has error while running on .AutoFilterMode = False

  9. #9
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Autofilter using Array & loop

    Fixed it, hope it may still help.
    Attached Files Attached Files

+ 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. Loop new messages containing a table, populate a dynamic array, paste array to Excel
    By laripa in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2013, 07:20 AM
  2. Loop or use autofilter to get my results
    By bopsgtir in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-13-2013, 06:33 AM
  3. Using an autofilter instead of LOOP
    By E3iron in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-11-2010, 12:55 PM
  4. loop through autofilter dropdown
    By Jack in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-01-2006, 07:38 AM
  5. Loop Macro autofilter
    By Paul. in forum Excel General
    Replies: 2
    Last Post: 03-25-2005, 06:07 AM

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