Loop through filtered values and copy the filtered array values to another sheet

krshnn

New Member
Joined
Mar 23, 2022
Messages
17
Office Version
  1. 2013
Platform
  1. Windows
I'm basically a noob when it comes to looping in excel.
I find the below issue seems to be more of a loop point. Kindly assist. Thanks in advance.

I have 22 columns with variable rows.
Here the 16th column needs to be filtered. 16th column name is "Status"
Say for example, Status column usually contains values of (for example) "Documents submitted to bank" , "Documents not submitted" ,"Payment not realized etc."

So what I would like to achieve is
1.Apply autofilter first -> Range("A1:V1").autofilter
2. Filter Status column for each value in that column like the above three example criteria stated above
3. Then copy visible cells (including header and all rows of that filtered value - basically variable rows by 22 columns) to a new sheet.

In case of any queries, kindly ask. Thanks in advance
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Give this a try with a copy of your workbook.
Assumes column Z is vacant to use as a helper column.

VBA Code:
Sub CopyToSheets()
  Dim r As Long
  
  Application.ScreenUpdating = False
  With ActiveSheet.Range("A1").CurrentRegion
    .Columns(16).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("Z1"), Unique:=True
    For r = 2 To .Range("Z" & Rows.Count).End(xlUp).Row
      .AutoFilter Field:=16, Criteria1:=.Range("Z" & r).Value
      Worksheets.Add After:=Sheets(Sheets.Count)
      .Copy Destination:=Range("A1")
    Next r
    .AutoFilter Field:=16
    Intersect(.EntireRow, .Parent.Columns("Z")).ClearContents
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thanks Sir for your valuable time and effort. It works as intended.
 
Upvote 0
You're welcome. Thanks for the feedback. :)
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top