Home
> service-is-programming > How to split an Excel workbook into one file per sheet with VBA (reworked)
How to split an Excel workbook into one file per sheet with VBA (reworked)
- There are other code snippets on stackoverflow.com, but I went with the top match in the Google search.
- Couldn’t get it to work (error # 424 Object required on xWS.copy in Excel 2013, with the VBA run from a separate utilities workbook) until I made these changes:
'TRP reworked http://www.extendoffice.com/documents/excel/628-excel-split-workbook.html
Sub Workbook_Split() 'Updated by TRP 20150924
Dim xPath As String
Dim myWorkSheet As Worksheet
xPath = Application.ActiveWorkbook.Path 'the source workbook needs be saved first
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWS In ActiveWorkbook.Sheets 'trp: replaced ThisWorkbook.Sheets
'trp: replaced xWS.Copy 'toask: this fails with 424: object required
Set myWorkSheet = xWS
myWorkSheet.Copy 'If you don't specify either Before or After, Microsoft Excel creates a new workbook that contains the copied sheet.
Application.ActiveWorkbook.SaveAs FileName:=xPath & "\" & myWorkSheet.Name & ".xlsx"
'trp: replaced xWS.Name & ".xls"
Application.ActiveWorkbook.Close False 'trp:savechanges:=False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Categories: service-is-programming
MS-Excel, VBA, worksheets


Thanks, it worked for me…