Exporting an Excel worksheet to a new workbook using VBA is a fast, automated way to save a copy of a specific sheet without affecting the original file. This is useful for sharing data, creating backups, or preparing reports. You only need Microsoft Excel with VBA access enabled.

What You'll Need

  • Microsoft Excel (any recent version that supports VBA, such as Excel 2010 or later)
  • A workbook containing the worksheet you want to export
  • Basic access to the VBA editor (Developer tab enabled or Alt+F11 shortcut)

Step-by-Step

  1. Open the Excel workbook that contains the sheet you want to export.
  2. Press Alt + F11 to open the VBA Editor. If the Developer tab is visible, you can also click Developer > Visual Basic.
  3. In the VBA Editor, go to Insert > Module to create a new module where you’ll paste the code.
  4. Copy and paste the following VBA code into the module window:
    Sub ExportSheetToNewWorkbook()
        Dim ws As Worksheet
        Dim newWb As Workbook
        
        ' Set the worksheet you want to export — change "Sheet1" to your sheet’s name
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        
        ' Create a new workbook
        Set newWb = Workbooks.Add
        
        ' Copy the worksheet to the new workbook
        ws.Copy Before:=newWb.Sheets(1)
        
        ' Optional: Close the default blank sheet that comes with new workbooks
        Application.DisplayAlerts = False
        newWb.Sheets(2).Delete
        Application.DisplayAlerts = True
        
        ' Prompt user to save the new workbook
        With Application.FileDialog(msoFileDialogSaveAs)
            .Title = "Save Exported Sheet As"
            .InitialFileName = ws.Name & ".xlsx"
            .FilterIndex = 2 ' Excel Workbook (*.xlsx)
            If .Show = -1 Then
                newWb.SaveAs .SelectedItems(1)
                MsgBox "Sheet exported successfully to:" & vbCrLf & .SelectedItems(1), vbInformation
            Else
                MsgBox "Export cancelled.", vbExclamation
            End If
        End With
        
        ' Clean up
        newWb.Close SaveChanges:=False
        Set ws = Nothing
        Set newWb = Nothing
    End Sub
  5. In the code, locate the line `Set ws = ThisWorkbook.Worksheets("Sheet1")` and replace "Sheet1" with the exact name of the worksheet you want to export (e.g., "SalesData" or "Report_Q3").
  6. Close the VBA Editor and return to Excel.
  7. To run the macro, press Alt + F8, select `ExportSheetToNewWorkbook`, and click Run.
  8. A file save dialog will appear. Choose a folder, optionally rename the file, and click Save.
  9. The macro will create a new workbook containing only the selected sheet, save it to your chosen location, and confirm success with a message box.

Common Mistakes

  • Using a sheet name that doesn’t exactly match (including spaces or capitalization) — VBA is exact about names.
  • Forgetting to disable alerts before deleting the default sheet, which can cause the macro to pause or fail if the user doesn’t respond.
  • Not changing the sheet name in the code, resulting in exporting the wrong sheet or an error if "Sheet1" doesn’t exist.
  • Running the macro from a macro-disabled workbook (.xlsx) instead of a macro-enabled one (.xlsm) — the code won’t persist or run.
  • Attempting to export a sheet that contains activeX controls, form controls, or external links that may not behave as expected in isolation.

Tips

  • Use a consistent naming convention for sheets to avoid errors when referencing them in code.
  • If you export sheets frequently, consider adding a button to the worksheet (Developer > Insert > Button) linked to this macro for one-click access.
  • To export multiple sheets, modify the code to loop through a list of sheet names or use `Array` to define which sheets to copy.
  • Always test the macro on a copy of your workbook first, especially if it contains sensitive data.
  • For automation, you can trigger this macro on workbook open, before save, or via a ribbon customization.
  • If you want to preserve formatting, formulas, and data validation, the `.Copy` method used here preserves all of these by default.

Troubleshooting

If the macro doesn’t work as expected, follow these steps in order:

  1. Macro not running: Ensure you saved the workbook as a macro-enabled file (.xlsm). If you saved it as .xlsx, the VBA code is lost. Re-save as .xlsm and re-paste the code if needed.
  2. "Subscript out of range" error: This means the sheet name in the code doesn’t exist. Double-check the exact sheet name (tabs at the bottom) and update the code accordingly. Remember that names are case-sensitive in practice and must match exactly, including spaces.
  3. File save dialog doesn’t appear: Check if macro security is blocking execution. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and enable macros (with notification). Restart Excel after changing.
  4. Exported file is blank or missing data: Confirm you copied the correct sheet and that it contains data. Also, ensure you didn’t accidentally delete the copied sheet before saving.
  5. Error when deleting default sheet: If you get an error deleting the second sheet, it may be because the new workbook only has one sheet (if copying failed). Verify the Copy step worked by stepping through the code with F8 in the VBA editor.
  6. Macro runs but nothing happens: Add `MsgBox "Macro started"` at the beginning and `MsgBox "Macro ended"` at the end to confirm execution flow. If you don’t see the messages, the macro isn’t being triggered.

Frequently Asked Questions

Can I export multiple sheets at once?

Yes. Replace the single `Set ws` line with a loop or use an array of sheet names, copying each one to the new workbook before saving.

Will this preserve formulas, formatting, and data validation?

Yes. The `.Copy` method duplicates the sheet exactly, including all formatting, formulas, data validation, comments, and row/column dimensions.

Do I need to enable macros every time I open the file?

If you save the workbook as .xlsm and have macro security set to notify, you’ll see a security warning when opening. Click Enable Content to run macros.

Can I automate this to run when I close the workbook?

Yes. Place the call to `ExportSheetToNewWorkbook` in the `Workbook_BeforeClose` event in the ThisWorkbook module to trigger it automatically.

Is there a way to avoid the save dialog and save automatically?

Yes. Replace the FileDialog block with a fixed path, e.g., `newWb.SaveAs "C:\Exports\" & ws.Name & ".xlsx"`, but ensure the folder exists and you have write permissions.