VBA & Macro Problems

Excel VBA: Copy and Rename a Table on a New Worksheet

John WilsonJohn Wilson Nov 25, 2026 869 views

Question details

Correct an Excel VBA macro that creates a worksheet, copies an existing table to it, and renames the copied table.

Copy and Rename an Excel Table on a New Worksheet with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user wants to copy the AllData table from a source worksheet to a new worksheet and give the copy a new name.
Observed behavior
Adding a worksheet makes it active, so an unqualified range reference can look for the source table on the new sheet instead.
Before you start

Confirm that the workbook containing the macro has a worksheet named Sheet1 and a table named AllData. Choose a new worksheet name and table name that are not already in use, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use Explicit Worksheet References to Copy and Rename the Table

Store the source worksheet before adding the new sheet, then use worksheet-qualified references for the copy and rename actions.

Worksheets.Add activates the new sheet. Qualifying the source range with wsSource prevents VBA from looking for AllData on that new sheet.

1
Open the VBA editor

In Excel, open Developer > Visual Basic, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub CopySheets_2() Dim wsSource As Worksheet Dim wsNewSheet As Worksheet Set wsSource = ThisWorkbook.Worksheets("Sheet1") Set wsNewSheet = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)) wsNewSheet.Name = "NewSheet" wsSource.Range("AllData[#All]").Copy Destination:=wsNewSheet.Range("A1") wsNewSheet.Range("A1").ListObject.Name = "MyNewName" End Sub

3
Adjust the names

In the code, replace Sheet1 and AllData with your source worksheet and table names. Replace NewSheet and MyNewName with unused names for the new worksheet and copied table.

4
Run the macro

In Excel, select Developer > Macros, choose CopySheets_2, and select Run. Check the new worksheet for the copied table.

Use Explicit Worksheet References to Copy and Rename the Table
Workbook scope: ThisWorkbook refers to the workbook containing the macro. If the source table is in another workbook, change the workbook references to point to that workbook.
Free Microsoft Office alternative

Work with Spreadsheet Tables in WPS Office

WPS Office is a free, lightweight alternative for everyday spreadsheet work. Its familiar interface and Excel format compatibility make it easier to move existing files, though it does not fix Excel VBA errors and macro support may differ.

Open and edit spreadsheet data in WPS SpreadsheetsCompatible with Microsoft Excel formats such as XLSXUse a familiar spreadsheet interface for everyday editingMove existing Office documents into a lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does my table reference fail after I add a worksheet?

Worksheets.Add makes the new sheet active. An unqualified Range reference may then look on that sheet, so use a source reference such as wsSource.Range("AllData[#All]").

How can I find the source table's name?

Select a cell in the table in Excel, then open the Table Design tab and check the Table Name field.

Why does the macro fail when I run it a second time?

The worksheet NewSheet or table MyNewName may already exist. Change those names in the code to unused names before running the macro again.

Where can I check the copied table's name?

Select a cell in the copied table, open Table Design, and look at the Table Name field. It should show the name assigned by the macro.