VBA & Macro Problems

How to Fix VBA Macro Failing to Copy Worksheets to a New Excel File

Steven ParkerSteven Parker Nov 2, 2026 869 views

Question details

The user is trying to execute a VBA macro to copy specific worksheets and save them as a new XLSX file, but the code fails or encounters errors during execution.

How to Troubleshoot a VBA Macro That Fails to Copy Worksheets to a New File
Product
Excel
Device & OS
not provided
Scenario
Running a VBA script to automatically export selected worksheets and save them as a separate Excel workbook.
Observed behavior
The macro fails to execute properly, which may be caused by workbook context errors, missing sheet names, file permission limits, or SaveAs conflicts with existing files.
Before you start

Before troubleshooting your VBA code, verify that the worksheets you are trying to copy actually exist in the source workbook (e.g., Sheet1, Sheet2, Sheet4) and that you have write permissions for the destination folder.

Solution 1Recommended

Update VBA Code with Timestamps and Workbook Context

Modifying the macro to explicitly reference the source workbook and adding a timestamp to the output filename prevents common saving and context errors.

When a macro copies sheets, it creates a new active workbook. If your code does not explicitly state where the original sheets are located using 'ThisWorkbook', Excel may look for them in the newly created blank workbook and fail. Additionally, saving a file with a static name repeatedly will cause a conflict if the file already exists.

1
Open the VBA Editor

Press ALT + F11 in Excel to open the Visual Basic for Applications (VBA) editor.

2
Locate your Macro

In the Project Explorer on the left, find and double-click the module containing your worksheet copying macro.

3
Explicitly reference the source workbook

Change your sheet references to use 'ThisWorkbook.Sheets' instead of just 'Sheets'. For example: ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet4")).Copy

4
Add a timestamp to the SaveAs filename

Modify the .SaveAs Filename parameter to include a dynamic timestamp format. For example: .SaveAs Filename:="D:\myVBA_" & Format(Now, "ddmmyyyy-hhmmss") & ".xlsx"

5
Define the correct file format

Ensure you include FileFormat:=xlOpenXMLWorkbook in your SaveAs method to properly save it as an XLSX file.

Update VBA Code with Timestamps and Workbook Context
Tip: Using 'Application.ScreenUpdating = False' at the beginning of your code and turning it back to 'True' at the end will prevent screen flickering and speed up the macro execution.

Run and Edit VBA Macros Seamlessly in WPS Spreadsheet

WPS Office provides robust support for Excel macros and VBA scripts. You can easily edit, troubleshoot, and run your VBA codes to copy worksheets just like you would in Microsoft Excel.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled workbook (.xlsm).
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon menu.
  3. 3. Open the VBA Editor: Click on 'Visual Basic' or press ALT + F11 to open the built-in VBA editor.
  4. 4. Run the Macro: Paste your worksheet copying code into a module and click 'Run' or press F5 to execute it.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .xls) formatsSupports standard VBA syntax and macro executionLightweight application that runs heavy macros quicklyFamiliar developer interface for editing VBA code
QA img-9

Frequently Asked Questions

Why does my VBA SaveAs method throw a 1004 runtime error?

A runtime error 1004 during a SaveAs operation usually occurs if the file path is incorrect, the destination folder is read-only, or a file with the exact same name is already open or exists and cannot be overwritten.

How do I copy multiple worksheets into a new workbook using VBA?

You can use an array with the .Copy method, such as ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Copy. When the Copy method is used without a destination parameter, it automatically creates a new active workbook containing only those copied sheets.

What is the difference between ActiveWorkbook and ThisWorkbook in VBA?

'ThisWorkbook' refers to the file where the VBA code is currently written and running. 'ActiveWorkbook' refers to the file that currently has focus in Excel, which changes dynamically when a new workbook is created via a macro.

Do I need to save the new workbook as a macro-enabled file (.xlsm)?

If the new workbook only contains the copied worksheets and does not need to contain any VBA code itself, you can safely save it as a standard Excel file (.xlsx) by using FileFormat:=xlOpenXMLWorkbook in your SaveAs code.