How do I save a specific sheet to a new workbook using Excel VBA?
I have multiple sheets with names "Sheet1", "Sheet2", "Sheet3" and so on.
I'd like to save all, in individual workbooks, with a single click.
This is returns an alert
Method Save as of object workbook failed
Sub SaveSplitSheet()
Dim ws As Worksheet
Dim wb As Workbook
For Each ws In ThisWorkbook.Sheets
If ws.Name Like "Sheet" & "*" Then
Application.DisplayAlerts = False
ws.Copy
ActiveWorkbook.SaveAs "/Users/Tukiyem/Downloads", FileFormat:=56
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End If
Next
End Sub
Found the answer-> the code below saves multiple sheets that contain name "sheet...." as individual workbooks.
Sub SaveAsInLoop()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name Like "Sheet" & "*" Then
Application.DisplayAlerts = False
ws.Copy
ActiveWorkbook.SaveAs "/Users/Tukiyem/Downloads/" & ws.Name & ".xlsx", FileFormat:=51
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End If
Next
End Sub
Worksheet
but then closing and saving theWorkbook
(which is what your worksheets are in). Generally if you are copying aWorksheet
you would want to put it in a newWorksheet
in the sameWorkbook
OR even in a whole newWorkbook
. If you simply want to save the workbook in a new location, you can just use the lineActiveWorkbook.SaveAs "/Users/Tukiyem/Downloads", FileFormat:=56
Though you are always better to use a full file path and include a/
at the end. – Samuel Everson