VBA & Macro Problems

How to Fix VBA Error 1004 When Copying Worksheets with Tables

Huda QurayshiHuda Qurayshi Oct 29, 2026 870 views

Question details

The user needs to fix Run-time error 1004 that occurs when using a VBA macro to copy multiple worksheets containing tables.

How to Fix VBA Error 1004 When Copying Worksheets with Tables
Product
Spreadsheet/Excel
Device & OS
not provided
Scenario
Running a VBA macro to copy a group of worksheets that contain formatted tables or are connected to SQL data.
Observed behavior
VBA raises Run-time error 1004, preventing the group of worksheets from being copied.
Before you start

Before modifying your VBA script, ensure you have saved a backup copy of your macro-enabled workbook in case you need to revert your code.

Solution 1Recommended

Copy Worksheets Individually via VBA Loop

Avoid the error by modifying your VBA code to iterate through and copy each worksheet one by one instead of copying the entire group simultaneously.

Run-time error 1004 often occurs due to memory limitations or object referencing issues when the spreadsheet application attempts to duplicate multiple ListObjects (tables) or SQL data connections at exactly the same time. Using a VBA loop to iterate through the sheets individually safely bypasses this limitation.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications editor in your active workbook.

2
Create a New Workbook via Code

In your macro module, initialize a new workbook object to serve as the destination for the copied sheets. You can do this by defining a variable like `Set newWb = Workbooks.Add`.

3
Loop and Copy Sheets Individually

Use a `For Each` loop to iterate through the specific worksheets in the source workbook. Inside the loop, copy them one at a time to the destination workbook using `ws.Copy After:=newWb.Sheets(newWb.Sheets.Count)`.

4
Remove Default Worksheets

Add a few lines of code to target and delete the default blank worksheets (such as 'Sheet1') that were automatically generated when the new destination workbook was created.

5
Save the New Workbook

Finish the macro by using the `SaveAs` method on your `newWb` object to save the newly created workbook containing all your successfully copied tables.

Copy Worksheets Individually via VBA Loop
Suppressing Deletion Alerts: When deleting the default worksheets via VBA, use `Application.DisplayAlerts = False` before the delete command, and set it back to `True` afterwards to prevent confirmation popups from interrupting the macro.
Powerful Spreadsheet Tool with Macro Support

Use WPS Spreadsheet to Run and Edit VBA Macros

WPS Spreadsheet provides excellent compatibility with advanced macros, allowing you to edit VBA code, automate repetitive tasks, and manage complex worksheets with tables seamlessly.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office from the official website and install it on your system.
  2. 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm or .xlsb workbook containing the tables.
  3. 3. Access the Developer Tab: Navigate to the Developer tab on the ribbon to open the VBA Editor and view your modules.
  4. 4. Run Your Modified Script: Execute your updated VBA script to copy the worksheets individually without encountering Error 1004.
Fully compatible with Microsoft Excel macro formats (.xlsm, .xlsb)Built-in developer tools for editing and running VBA macros smoothlyLightweight application that performs quickly even with heavy data connectionsFamiliar user interface requiring zero learning curve for VBA developers
QA img-9

Frequently Asked Questions

Why does Error 1004 happen when copying grouped sheets with tables?

This error typically occurs because the application struggles to simultaneously duplicate complex objects like ListObjects (tables) or external SQL data connections across multiple sheets at the exact same time, triggering a run-time failure.

Can I copy multiple sheets manually without getting this VBA error?

Yes, manually selecting multiple worksheet tabs and right-clicking to copy them usually works fine. The Run-time error 1004 in this context is specifically a programmatic limitation when VBA attempts to execute the batch duplication of tables.

How do I suppress the deletion warning when removing default sheets in my macro?

You can suppress the warning by adding `Application.DisplayAlerts = False` immediately before your `Sheet.Delete` command, and then restoring it by adding `Application.DisplayAlerts = True` right after the deletion line.