VBA & Macro Problems

VBA Code to Save Selected Excel Worksheets as a New Workbook

Elise WilliamsElise Williams Nov 3, 2026 869 views

Question details

The user wants a VBA macro to copy only the currently selected worksheets from a large template and save them as a new workbook with a dynamically generated filename.

How to Save Selected Excel Worksheets as a New Workbook Using VBA
Product
Excel
Device & OS
not provided
Scenario
Extracting specific worksheets from an Excel file and automatically saving them as a new file, using cell values and the current date to name the new file.
Observed behavior
The user needs the exact VBA syntax to efficiently copy the ActiveWindow.SelectedSheets, assign the dynamic filename, save the new file, and properly close it.
Before you start

Before running any VBA code, ensure you have enabled the Developer tab in your spreadsheet application and saved your main template as a Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use VBA SaveAs Method to Export Selected Sheets

This recommended macro copies the selected sheets to a new workbook and uses the SaveAs method to explicitly assign a dynamic filename based on a cell value and the current date.

Using the SaveAs method gives you full control over the file format and ensures the newly created workbook is safely saved to the exact path of your original template.

1
Open the VBA Editor

Press the 'Alt + F11' keys on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu, then select 'Module' to create a blank script window.

3
Paste the Macro Code

Paste the following code: Sub SaveSelectedSheets() Dim folderPath As String, fileName As String folderPath = ThisWorkbook.Path fileName = ActiveWindow.SelectedSheets(1).Range("A1").Value & "_" & Format(Date, "yyyymmdd") & ".xlsx" Application.ScreenUpdating = False ActiveWindow.SelectedSheets.Copy ActiveWorkbook.SaveAs Filename:=folderPath & "\" & fileName, FileFormat:=xlOpenXMLWorkbook ActiveWorkbook.Close SaveChanges:=False Application.ScreenUpdating = True End Sub

4
Run the Macro

Select the sheets you want to export in your main workbook, return to the VBA editor, and press 'F5' to run the code.

Use VBA SaveAs Method to Export Selected Sheets
Check for Broken Links: Test the macro on a backup copy first. Ensure the selected sheets do not contain formulas that depend on unselected sheets, as these will turn into external links in the new workbook.
Automate Tasks in WPS Office

Run Excel VBA Macros Smoothly in WPS Office

WPS Office provides robust support for VBA macros, allowing you to run, edit, and create automation scripts just like in Microsoft Excel. You can seamlessly use the exact same VBA code to save selected worksheets as a new workbook without any modifications.

  1. 1. Download WPS Office: Install WPS Office and open your macro-enabled spreadsheet.
  2. 2. Access Developer Tools: Navigate to the 'Tools' tab on the ribbon and click on 'Developer' to reveal the macro features.
  3. 3. Open the VBA Editor: Click 'VBA Editor' to paste your worksheet extraction code into a new module.
  4. 4. Execute Your Automation: Select the tabs you wish to copy and run the macro to instantly generate your new Excel file.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .xls) formats.Built-in VBA editor for running and debugging macros natively.Lightweight alternative with a highly familiar spreadsheet interface.Completely free to use for daily spreadsheet operations and document editing.
microsoft office alternative - wps office

Frequently Asked Questions

How do I select multiple worksheets before running the VBA macro?

You can select multiple discrete sheets by holding down the 'Ctrl' key and clicking the individual sheet tabs at the bottom of the window. To select a continuous range, click the first sheet tab, hold the 'Shift' key, and click the last sheet tab.

Why does my macro fail when saving the new workbook?

This usually happens if the dynamically generated filename contains invalid characters (such as slashes, colons, asterisks, or question marks) derived from the cell value. It can also occur if the target folder path does not exist or lacks write permissions.

Will formulas in the selected sheets still work in the newly saved workbook?

If the formulas only reference cells within the copied sheets, they will work perfectly. However, if they reference sheets that were not copied to the new workbook, they will automatically convert into external links pointing back to your original template.