VBA: Separate worksheets into new workbooks and save into a folder

ManhattanJM

New Member
Joined
Feb 19, 2022
Messages
6
Office Version
  1. 2016
Platform
  1. MacOS
Hi,

I'm trying to take a workbook and separate all of its sheets into new, inidividual workbooks and save them down in a folder with the current name of the sheet. I found code online (below) to do this, but I'm stumped with a run-time error '1004': Method 'SaveAs' of object '_Workbook' failed.

When I hit end and go back to the workbook, all of the sheets are blank (still have names) and don't even have gridlines anymore. When I close out of the workbook, a new file with a generic name opens up with the contents of my first tab but that's it. No additional tabs and nothing is saved down. If someone could point me in the right direction, that would be much appreciated.

Thank you!

VBA Code:
Sub Splitbook()


Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ActiveWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs FileName:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
During debug, it highlights the below line:

VBA Code:
Application.ActiveWorkbook.SaveAs FileName:=xPath & "\" & xWs.Name & ".xlsx"
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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