I want to Autofilter multiple sheets and copy and save the data to new workbook with range A2 value as file name.
Iam attaching the zip file containing sample and output files for your reference.
any help will be appreciated.
Thanks in advance.
I want to Autofilter multiple sheets and copy and save the data to new workbook with range A2 value as file name.
Iam attaching the zip file containing sample and output files for your reference.
any help will be appreciated.
Thanks in advance.
Re: Autofilter multiple sheets and copy & save to new workbook
Are you still looking for a solution here? I actually had code written yesterday and was about to post it here and Excel crashed on me
Lost it all.
If you still need it, I will give it a go again today.
Ger
Re: Autofilter multiple sheets and copy & save to new workbook
Thank you Ger,
Yes, I still need it .
Re: Autofilter multiple sheets and copy & save to new workbook
Code below and attached. Run Macro "Generate_Output"... seems to work well for the sample data that you provided... HTH
Option Explicit
Public Sub Generate_Output()
Dim vList As Variant
Dim rData As Range
Dim ws As Worksheet
Dim iloop As Integer
Dim wbNew As Workbook
Dim r2 As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error GoTo My_Exit
Set rData = Worksheets(1).UsedRange.Columns(1)
vList = Application.Transpose(rData.Offset(1, 0).Resize(rData.Rows.Count - 1))
vList = RemoveDupes(vList)
For Each ws In ThisWorkbook.Worksheets
Set r2 = ws.UsedRange
For iloop = LBound(vList) To UBound(vList)
Set wbNew = Nothing
On Error Resume Next: Set wbNew = Workbooks(vList(iloop) & ".xlsx"): On Error GoTo My_Exit
If wbNew Is Nothing Then
Set wbNew = Workbooks.Add
ws.UsedRange.Rows(1).Copy Destination:=wbNew.Worksheets(1).Range("A1")
wbNew.SaveAs ThisWorkbook.Path & "\" & vList(iloop), 51
End If
r2.Rows(1).AutoFilter field:=1, Criteria1:=vList(iloop)
r2.Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy Destination:=wbNew.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Next
ws.AutoFilterMode = False
Next
For iloop = LBound(vList) To UBound(vList)
Workbooks(vList(iloop) & ".xlsx").Close True
Next
MsgBox "All Done!", vbOKOnly
My_Exit:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
If Err.Number <> 0 Then MsgBox "OH NO!!! Something bad happened" & vbCrLf & vbCrLf & Err.Number & vbCrLf & vbCrLf & Err.Description & vbCrLf & vbCrLf & "....And make sure all the excel files are closed", vbOKOnly
End Sub
Public Function RemoveDupes(InputArray As Variant) As Variant
'remove duplicates from array
Dim x As Long
If IsArray(InputArray) Then
With CreateObject("Scripting.Dictionary")
For x = LBound(InputArray) To UBound(InputArray)
If Not IsMissing(InputArray(x)) Then .Item(InputArray(x)) = 1
Next
RemoveDupes = .Keys
End With
Else
RemoveDupes = InputArray
End If
End Function
Display More
Re: Autofilter multiple sheets and copy & save to new workbook
Thanks a ton Ger,
This is Exactly what I wanted.
Really appreciate your effort.
:hyper2:
Re: Autofilter multiple sheets and copy & save to new workbook
Your welcome. Thanks for the feedback.
I forgot to mention that - if you run the macro and open one of the output workbooks, and try to run the macro again, it will fail because the output workbook is still open and can not be updated/overwritten (you will get a message box telling you something went wrong). So just ensure that all output workbooks are closed when you run it. I could code around that issue if it becomes a problem... but for now, its just easier for me to warn you
Ger
Re: Autofilter multiple sheets and copy & save to new workbook
Thanks & Noted
Don’t have an account yet? Register yourself now and be a part of our community!