+ Reply to Thread
Results 1 to 12 of 12

Loop through Auto Filter Criteria and Copy Formula Results Into New Sheet

Hybrid View

  1. #1
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through Auto Filter Criteria and Copy Formula Results Into New Sheet

    Hi hamidxa

    Replace the Code with this
    Option Explicit
    
    Sub Filter_Stuff()
      Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet
      Dim LR As Long, LC As Long
      Dim cel As Range, Rng As Range
    
      Application.ScreenUpdating = False
      If Not Evaluate("ISREF(Lists!A1)") Then
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Lists"
      Else
        Sheets("Lists").Cells.Clear
      End If
    
      Set ws = Sheets("FW15")
      Set ws1 = Sheets("Lists")
      Set ws2 = Sheets("CopiedResults")
    
      With ws2
        .UsedRange.Offset(1, 0).Clear
      End With
    
      With ws
        LC = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                         SearchDirection:=xlPrevious).Column
        LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                         SearchDirection:=xlPrevious).Row
        .Range("A3:A" & LR).AdvancedFilter Action:=xlFilterCopy, _
                                           CopyToRange:=ws1.Range("A1"), Unique:=True
        ActiveWorkbook.Names.Add Name:="AAA", RefersTo:= _
                                 "=OFFSET(Lists!$A$2,0,0,(COUNTA(Lists!$A:$A)-1),1)"
        ws1.Range("AAA").Sort Key1:=ws1.Range("A2"), Order1:=xlAscending, _
                              Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        ws1.Range("AAA").Copy ws2.Range("A2")
    
        .Range("B3:B" & LR).AdvancedFilter Action:=xlFilterCopy, _
                                           CopyToRange:=ws1.Range("B1"), Unique:=True
        ActiveWorkbook.Names.Add Name:="BBB", RefersTo:= _
                                 "=OFFSET(Lists!$B$2,0,0,(COUNTA(Lists!$B:$B)-1),1)"
        ws1.Range("BBB").Sort Key1:=ws1.Range("B2"), Order1:=xlAscending, _
                              Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        ws1.Range("BBB").Copy ws2.Range("C2")
    
        .Range("C3:C" & LR).AdvancedFilter Action:=xlFilterCopy, _
                                           CopyToRange:=ws1.Range("C1"), Unique:=True
        ActiveWorkbook.Names.Add Name:="CCC", RefersTo:= _
                                 "=OFFSET(Lists!$C$2,0,0,(COUNTA(Lists!$C:$C)-1),1)"
        ws1.Range("CCC").Sort Key1:=ws1.Range("C2"), Order1:=xlAscending, _
                              Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
        ws1.Range("CCC").Copy ws2.Range("E2")
    
        If Not .AutoFilterMode Then
          .Rows("3:3").AutoFilter
        End If
    
        Set Rng = ws2.Range(("A2"), ws2.Range("A2").End(xlDown))
    
        For Each cel In Rng
          .Range(.Cells(3, 1), .Cells(LR, LC)).AutoFilter field:=1, Criteria1:=cel.Value
          .Range(.Cells(3, 1), .Cells(LR, LC)).AutoFilter field:=9, Criteria1:="Compliant"
          ws2.Range(cel.Address).Offset(0, 1).Value = .Range("F2").Text
        Next cel
        .AutoFilterMode = False
    
        Set Rng = ws2.Range(("C2"), ws2.Range("C2").End(xlDown))
    
        For Each cel In Rng
          .Range(.Cells(3, 1), .Cells(LR, LC)).AutoFilter field:=2, Criteria1:=cel.Value
          .Range(.Cells(3, 1), .Cells(LR, LC)).AutoFilter field:=9, Criteria1:="Compliant"
          ws2.Range(cel.Address).Offset(0, 1).Value = .Range("F2").Text
        Next cel
        .AutoFilterMode = False
    
        Set Rng = ws2.Range(("E2"), ws2.Range("E2").End(xlDown))
        For Each cel In Rng
          .Range(.Cells(3, 1), .Cells(LR, LC)).AutoFilter field:=3, Criteria1:=cel.Value
          .Range(.Cells(3, 1), .Cells(LR, LC)).AutoFilter field:=9, Criteria1:="Compliant"
          ws2.Range(cel.Address).Offset(0, 1).Value = .Range("F2").Text
        Next cel
        .AutoFilterMode = False
    
      End With
      Application.DisplayAlerts = False
      Sheets("Lists").Delete
      Application.DisplayAlerts = True
      Application.CutCopyMode = False
      Application.ScreenUpdating = True
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  2. #2
    Forum Contributor
    Join Date
    05-24-2012
    Location
    Nashville, TN
    MS-Off Ver
    Excel 2007
    Posts
    113

    Re: Loop through Auto Filter Criteria and Copy Formula Results Into New Sheet

    jaslake,

    Truly grateful for all of your brilliance and consideration.
    This is simply awesome.
    Can't explain all of it, but i'll walk through the code and try to sort things out on my own at some point, but I definitely can see what you did there with the added criteria request.

    Again, a huge thank you!

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through Auto Filter Criteria and Copy Formula Results Into New Sheet

    You're welcome...glad I could help. Thanks for the Rep.

    If this resolves your issue please mark your Thread as SOLVED.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:
    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click SaveYou're welcome...glad I could help.

+ 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] Run a loop to slected data then filter on another sheet and copy to another sheet
    By af_lel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-21-2013, 07:10 AM
  2. [SOLVED] Filter a sheet and copy results to another sheet
    By dixiV in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-04-2012, 10:21 AM
  3. Filter a sheet and copy results to another sheet
    By dixiV in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-03-2012, 06:35 AM
  4. Auto filter data and copy results to another worksheet?
    By thisiscrazy in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-24-2012, 07:46 AM
  5. Replies: 2
    Last Post: 06-28-2011, 10:19 AM

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