Hi guys,
I need to split a excel file in multiple sheets based on column W. Every sheets must be named based on split criteria.
After that I want to save the resulting sheets in a specific location.
Thx
Hi guys,
I need to split a excel file in multiple sheets based on column W. Every sheets must be named based on split criteria.
After that I want to save the resulting sheets in a specific location.
Thx
Some suggestions you may fund useful:
- Our answerers are looking to "help" more often than they are looking to "do an entire project for you for free".
- Jump into this project yourself, when you get stuck on ONE specific hurdle, post that ONE item as a forum question.
- Show us what you've tried in an attached sample workbook and how it's not working for you. When you're doing the work yourself, just writing out a complete example question in a forum often points you to solution before you even finish posting.
- If you can't make any headway at all and no help is forthcoming in the free forum(s), we do have a Commercial Services forum where you can pay for assistance directly from our EF gurus."
It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file. Do you want each new sheet to be saved as a separate file? What is the full path to the folder where you want to save the files?
You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
Practice makes perfect. I'm very far from perfect so I'm still practising.
Thank you for your reply.
I can't provide the file, contains some confidential data but I can share my work so far (you will find it below). My approach was to filter the column based on specific criteria, copy filtered cells to another sheet, save that sheet to a specific location, then clear filters and repeat the procedure for another filter criteria.
I am stuck when one of the filter criteria is missing, I don't know how to skip part of the code when a certain filter criteria is missing.
How to skip this part of the code when filter criteria NCRT is missing in column W
![]()
Range("W1").AutoFilter 23, "=NCRT" Cells.Select Selection.Copy Sheets.Add After:=ActiveSheet Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Columns("A:A").EntireColumn.AutoFit Cells.Select Cells.EntireColumn.AutoFit Range("B1").Select Set Target = Range("W2") Application.Sheets(2).Name = VBA.Left(Target, 31) SaveNameA = Sheets(2).Range("W2").Text ActiveWorkbook.Sheets(2).Columns("W:W").Delete ActiveWorkbook.Sheets(2).Copy Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx" ActiveWorkbook.Close savechanges:=False Application.DisplayAlerts = False ActiveWorkbook.Sheets(2).Delete Application.DisplayAlerts = True
My entire code
![]()
Sub MyProject() Dim SaveNameA As String Range("W1").AutoFilter 23, "=NCRT" Cells.Select Selection.Copy Sheets.Add After:=ActiveSheet Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Columns("A:A").EntireColumn.AutoFit Cells.Select Cells.EntireColumn.AutoFit Range("B1").Select Set Target = Range("W2") Application.Sheets(2).Name = VBA.Left(Target, 31) SaveNameA = Sheets(2).Range("W2").Text ActiveWorkbook.Sheets(2).Columns("W:W").Delete ActiveWorkbook.Sheets(2).Copy Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx" ActiveWorkbook.Close savechanges:=False Application.DisplayAlerts = False ActiveWorkbook.Sheets(2).Delete Application.DisplayAlerts = True If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False Range("W1").AutoFilter 23, "=INT" Cells.Select Selection.Copy Sheets.Add After:=ActiveSheet Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Columns("A:A").EntireColumn.AutoFit Cells.Select Cells.EntireColumn.AutoFit Range("B1").Select Set Target = Range("W2") Application.Sheets(2).Name = VBA.Left(Target, 31) SaveNameA = Sheets(2).Range("W2").Text ActiveWorkbook.Sheets(2).Columns("W:W").Delete ActiveWorkbook.Sheets(2).Copy Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx" ActiveWorkbook.Close savechanges:=False Application.DisplayAlerts = False ActiveWorkbook.Sheets(2).Delete Application.DisplayAlerts = True If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False Range("W1").AutoFilter 23, "=REV" Cells.Select Selection.Copy Sheets.Add After:=ActiveSheet Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Columns("A:A").EntireColumn.AutoFit Cells.Select Cells.EntireColumn.AutoFit Range("B1").Select Set Target = Range("W2") Application.Sheets(2).Name = VBA.Left(Target, 31) SaveNameA = Sheets(2).Range("W2").Text ActiveWorkbook.Sheets(2).Columns("W:W").Delete ActiveWorkbook.Sheets(2).Copy Application.ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & SaveNameA & ".xlsx" ActiveWorkbook.Close savechanges:=False Application.DisplayAlerts = False ActiveWorkbook.Sheets(2).Delete Application.DisplayAlerts = True If Sheet1.AutoFilterMode Then Sheet1.AutoFilterMode = False End Sub
It looks like your code can be shortened. Could you de-sensitize the confidential data and attach a copy of the file? You don't necessarily need to include all the data, just enough rows to make it exactly representative of your actual file. I think the solution can be simple but I can't test it unless I have a file to work with.
I have attached the file.
Thx.
Try:
If you need to add another filter criteria, simply add it to the array in the code.![]()
Sub MyProject() Application.ScreenUpdating = False Dim arr As Variant, i As Long arr = Array("NCRT", "INT", "REV") For i = LBound(arr) To UBound(arr) If WorksheetFunction.CountIf(Range("W:W"), arr(i)) > 0 Then With ActiveSheet .Cells(1, 1).CurrentRegion.AutoFilter 23, arr(i) .AutoFilter.Range.Copy Sheets.Add After:=ActiveSheet Cells(1, 1).PasteSpecial xlPasteValues Cells.EntireColumn.AutoFit Range("B1").Select With ActiveSheet .Name = Left(arr(i), 31) .Columns("W:W").Delete .Copy With ActiveWorkbook .SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr(i) & ".xlsx" .Close False End With End With Application.DisplayAlerts = False Sheets(2).Delete Application.DisplayAlerts = True End With End If Next i Range("A1").AutoFilter Application.ScreenUpdating = False End Sub
You are very welcome.![]()
Hi Mumps1,
I really appreciated your help with my request. I have another question, it is possible, adapting the script from you, to split the resulting files based on the currency?. The currency can be EUR, CHF, USD and will always be in column G (the currency may differ from file to file, in one file it may be only CHF and EUR, in another file it may be CHF, EUR and USD).
The resulting files I would like to be named like this: "INT EUR", "INT CHF" and so on. The files I want to be saved in C:\Users\Home\Desktop\My Folder\Currency. I updated the test file with currency in column G.
THX
Should it be new files or new sheets?
![]()
Sub VenA() c00 = "E:\Temp\" Application.ScreenUpdating = False With Sheets("W").Cells(1).CurrentRegion .Columns(23).AdvancedFilter xlFilterCopy, , .Parent.Range("Z1"), True ar = .Parent.Cells(1, 26).CurrentRegion .Parent.Cells(1, 26).CurrentRegion.Clear If IsArray(ar) Then For j = 2 To UBound(ar) .AutoFilter 23, ar(j, 1) .Copy With Workbooks.Add.Sheets(1) .Paste .Name = Left(ar(j, 1), 23) .Columns.AutoFit .Columns(23).Delete .Parent.SaveAs c00 & ar(j, 1), 51 .Parent.Close 0 End With Next j .AutoFilter End If End With End Sub
Do you still want to split and save the data based on "NCRT", "INT", "REV" and then split each resulting file based on currency or just split and save on currency without splitting on "NCRT", "INT", "REV"?
Try:
![]()
Sub MyProject() Application.ScreenUpdating = False Dim arr1 As Variant, arr2 As Variant, i As Long, ii As Long arr1 = Array("NCRT", "INT", "REV") arr2 = Array("EUR", "CHF", "USD") For i = LBound(arr1) To UBound(arr1) If WorksheetFunction.CountIf(Range("W:W"), arr1(i)) > 0 Then With ActiveSheet .Cells(1, 1).CurrentRegion.AutoFilter 23, arr1(i) .AutoFilter.Range.Copy Sheets.Add After:=ActiveSheet Cells(1, 1).PasteSpecial xlPasteValues Cells.EntireColumn.AutoFit Range("B1").Select With ActiveSheet .Name = Left(arr1(i), 31) .Columns("W:W").Delete .Copy With ActiveWorkbook Application.DisplayAlerts = False .SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr1(i) & ".xlsx" Application.DisplayAlerts = True With .Sheets(Left(arr1(i), 31)) For ii = LBound(arr2) To UBound(arr2) If WorksheetFunction.CountIf(.Range("G:G"), arr2(ii)) > 0 Then .Cells(1, 1).CurrentRegion.AutoFilter 7, arr2(ii) .AutoFilter.Range.Copy Sheets.Add After:=ActiveSheet Cells(1, 1).PasteSpecial xlPasteValues Cells.EntireColumn.AutoFit Range("B1").Select .Name = Left(arr1(i), 31) & " " & arr2(ii) .Copy Application.DisplayAlerts = False ActiveWorkbook.SaveAs Filename:="C:\Users\Home\Desktop\My Folder\" & arr1(i) & " " & arr2(ii) & ".xlsx" ActiveWorkbook.Close False Sheets(2).Delete Application.DisplayAlerts = True End If Next ii End With End With ActiveWorkbook.Close False End With Application.DisplayAlerts = False Sheets(2).Delete Application.DisplayAlerts = True End With End If Next i Range("A1").AutoFilter Application.ScreenUpdating = False End Sub
Last edited by Mumps1; 04-27-2020 at 11:38 AM.
It is not necessary to quote full messages. You apparently haven't looked at the code in # 8. This is much more efficient and dynamic.
It is not necessary to always create a tab and delete it.
Someone with more posts doesn't necessarily write better code![]()
Sub VenA() Dim c00 As String, j As Long, ar Application.ScreenUpdating = False c00 = "E:\Temp\Temp1\" '"C:\Users\Home\Desktop\My Folder\" With Sheets("W").Cells(1).CurrentRegion Sheets.Add.Name = "Temp" Cells(1).Resize(, 2) = Array("WWW", "CUU") .AdvancedFilter xlFilterCopy, , Range("A1:B1"), True ar = Cells(1).CurrentRegion Application.DisplayAlerts = False Sheets("Temp").Delete For j = 2 To UBound(ar) .AutoFilter 23, ar(j, 1) .AutoFilter 7, ar(j, 2) .Copy With Workbooks.Add.Sheets(1) .Paste .Name = Left(ar(j, 1), 23) .Columns.AutoFit .Columns(23).Delete .Parent.SaveAs c00 & ar(j, 1) & "_" & ar(j, 2), 51 .Parent.Close 0 End With Next j .AutoFilter End With End Sub
![]()
Thanks guys, both solutions provided work very well. I am grateful to you.
The macro suggested by Vraag en antwoord is excellent. However, it doesn't create the workbooks based on "NCRT", "INT", "REV" without splitting based on currency.
@Mumps1,
They seemed unnecessary to me. But of course it can be adjusted.![]()
I suppose that the OP will have to decide if he/she needs the other files.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks