Excel VBA: Copy and Rename a Table on a New Worksheet
Question details
Correct an Excel VBA macro that creates a worksheet, copies an existing table to it, and renames the copied table.

- 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.
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).
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.
In Excel, open Developer > Visual Basic, then select Insert > Module.
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
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.
In Excel, select Developer > Macros, choose CopySheets_2, and select Run. Check the new worksheet for the copied table.

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.

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.




