VBA macros · Workbooks and files

VBA macro to save each sheet as a separate workbook in Excel

Save every visible sheet of a workbook as its own .xlsx file in the same folder, named after the sheet. Optional: values only, no links.

Windows + Mac

When one workbook holds a sheet per branch, per month or per client, you sometimes need each sheet as a separate file. Right-click › Move or Copy › new book › Save As, repeated thirty times, takes a long time. This macro copies each visible sheet into a new workbook and saves it as Sheet name.xlsx next to the original.

VBA
Option Explicit

Sub SplitSheetsIntoWorkbooks()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim folder As String
    Dim n As Long

    Set wb = ActiveWorkbook
    If Len(wb.Path) = 0 Then
        MsgBox "Save the workbook first; the files go into its folder.", vbExclamation
        Exit Sub
    End If
    folder = wb.Path & Application.PathSeparator

    On Error GoTo Fail
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    For Each ws In wb.Worksheets
        If ws.Visible = xlSheetVisible Then
            ws.Copy                                  ' copy into a brand-new workbook
            With ActiveWorkbook
                .SaveAs Filename:=folder & CleanName(ws.Name) & ".xlsx", _
                        FileFormat:=xlOpenXMLWorkbook
                .Close SaveChanges:=False
            End With
            n = n + 1
        End If
    Next ws

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox n & " file(s) saved in " & folder, vbInformation
    Exit Sub

Fail:
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    MsgBox "Stopped at sheet '" & ws.Name & "': " & Err.Description, vbExclamation
End Sub

Private Function CleanName(ByVal s As String) As String
    ' Replaces characters that are not allowed in file or sheet names
    Dim ch As Variant
    For Each ch In Array("\", "/", ":", "*", "?", """", "<", ">", "|", "[", "]")
        s = Replace(s, ch, "_")
    Next ch
    CleanName = Left$(Trim$(s), 31)
End Function
Save each visible sheet as its own workbook

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick SplitSheetsIntoWorkbooks and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

ws.Copy with no Before or After argument copies the sheet into a new, unsaved workbook that contains only that sheet, with its formatting, column widths, page setup and charts. The new workbook becomes the active workbook, which is why the next lines refer to ActiveWorkbook.

SaveAs with FileFormat:=xlOpenXMLWorkbook (the number 51) saves as a normal .xlsx file. The file name is the sheet name passed through CleanName, which replaces characters that Windows or macOS do not allow in file names. Then the new workbook is closed and the loop moves on.

Hidden sheets are skipped because Excel cannot copy a hidden sheet into a new workbook on its own. DisplayAlerts = False suppresses two questions: whether to replace an existing file, and whether to save a sheet that contained code as .xlsx. The error handler switches alerts back on so Excel does not stay silent afterwards. See Workbooks and worksheets in VBA and Files and folders: open, save, loop through files and write text files.

Change it

If the sheets contain formulas that refer to other sheets, the copies will contain links back to the original file. To send clean values only, convert before saving:

VBA
            ws.Copy
            With ActiveWorkbook
                .Worksheets(1).UsedRange.Value2 = .Worksheets(1).UsedRange.Value2
                .SaveAs Filename:=folder & CleanName(ws.Name) & ".xlsx", _
                        FileFormat:=xlOpenXMLWorkbook
                .Close SaveChanges:=False
            End With
Save values only, so the files have no external links

To save as PDF instead of Excel files, see VBA macro to save each sheet as a separate PDF in Excel. To save as CSV, change the extension to .csv and the format to xlCSVUTF8 (Excel 2016 and later) or xlCSV. To save into a subfolder, build the path with MkDir as shown in VBA macro to save a backup copy with a timestamp in Excel. If the sheets should be split by a column value instead, use VBA macro to split a sheet into workbooks by column value.

What can go wrong

Watch out. Files with the same name in the folder are overwritten without warning because alerts are off. Run the macro on a copy, or into an empty folder, if that matters.

Sheet-level defined names, data validation lists that point to other sheets and conditional formatting with references to other sheets also carry links or break in the copies. Check one copy with Data › Edit Links before sending a whole batch. Chart sheets are not included because the loop runs over Worksheets.

Tip. Works on Windows and Mac. A workbook on OneDrive may return a web address as its path; if SaveAs fails, open the file from the synced folder on your disk instead.

Related: VBA macro to split a sheet into workbooks by column value, VBA macro to save each sheet as a separate PDF in Excel, VBA macro to combine all sheets into one sheet in Excel.

More vba macros