VBA & Macro Problems

Create a New Excel Workbook from Selected Worksheets with VBA

Muhammad TalhaMuhammad Talha Nov 27, 2026 869 views

Question details

Create a new workbook containing selected worksheets or worksheets whose names match specified prefixes.

Create a New Excel Workbook from Selected Worksheets with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook contains many worksheets, and the user wants to copy a subset based on selection or name prefixes such as AB1, AB2, and AB3.
Observed behavior
The desired result is a separate workbook containing the matching worksheets while the original worksheets remain intact.
Before you start

Open the source workbook in desktop Excel and enable macros if prompted. For the prefix method, identify the worksheet containing the criteria in B2 and B3, and choose a new file name for the output.

Solution 1Recommended

Copy selected Excel worksheets to a new workbook

Use this method when you want to choose the worksheets by clicking their sheet tabs.

1
Select the worksheets

In the source workbook, select the worksheet tabs you want to copy. Use Ctrl-click for individual tabs or Shift-click for a range of tabs.

2
Add the macro

Press Alt+F11 to open the VBA editor, choose Insert > Module, and paste this code: Sub CopySelectedWorksheets() Dim src As Workbook Dim sh As Object Dim names As Variant Dim n As Long Dim path As Variant Set src = ActiveWorkbook For Each sh In ActiveWindow.SelectedSheets If TypeName(sh) = "Worksheet" Then n = n + 1 ReDim Preserve names(1 To n) names(n) = sh.Name End If Next sh If n = 0 Then MsgBox "Select at least one worksheet." Exit Sub End If path = Application.GetSaveAsFilename( _ InitialFileName:="SelectedSheets.xlsx", _ FileFilter:="Excel Workbook (*.xlsx), *.xlsx") If VarType(path) = vbBoolean Then Exit Sub src.Worksheets(names).Copy ActiveWorkbook.SaveAs Filename:=CStr(path), FileFormat:=xlOpenXMLWorkbook MsgBox "The selected worksheets were copied and saved." End Sub

3
Run and save

Return to Excel, keep the source workbook active, and run CopySelectedWorksheets from the Macros dialog. Choose a new file name and location in the Save As dialog.

Copy selected Excel worksheets to a new workbook
About the copied workbook: The original worksheets are not deleted. The output is an .xlsx file, which cannot retain VBA macros; copied formulas may also contain links to the source workbook.
Free Microsoft Office alternative

Manage Excel workbooks with WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing spreadsheets. Its format support helps you move existing workbooks between WPS Spreadsheet and Microsoft Excel; check macro support before relying on an Excel VBA workflow.

Open and edit Excel .xlsx and .xls workbooksWork with multiple worksheets in a familiar spreadsheet interfaceMove compatible spreadsheet files between WPS and Microsoft OfficeUse a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Will these macros delete any worksheets from the source workbook?

No. Both macros use the worksheet Copy method to create a separate workbook and do not delete source worksheets.

What happens if B2 or B3 is blank?

The prefix macro ignores a blank criterion. If both cells are blank, it displays a message and stops without copying any worksheets.

How can I copy sheets named AB1, AB2, and AB3?

Enter AB in B2 on the criteria worksheet, update the Sheet1 reference in the prefix macro if needed, and run CopyWorksheetsByPrefix. It copies all worksheet names that start with AB, not just those three names.

Why are macros missing from the new workbook?

The examples save the output as .xlsx, a format that cannot retain VBA macros. If the copied workbook needs macros, use a macro-enabled .xlsm format and adjust the Save As filter and file format in the code.

Can copied formulas still refer to the original workbook?

Yes. Formulas that refer to sheets or data not copied into the new workbook may retain external links. Check the formulas and links in the saved workbook.