VBA to replace named table, without changing table name

axf80

New Member
Joined
Oct 8, 2015
Messages
13
Hi,

I'm trying to use vba to update a table in my spreadsheet. Each time I run the macro it copies a table form workbook 1, called "Table1", into the same position in my new workbook. I then have some formulas in the second workbook that calculate things from the table. These formulas refer to the table name and the column headers - they need to be like this because the order of columns might change each week.

My problem is that each time the macro runs and replaces the table in workbook2 it changes the name of the table, and my formulas therefore break - all the references to Table1 get turned into REFS!.

Here's the code I use to copy and past the tables:

Sheets("workbook1").Range("Table1[#All]").Copy Sheets("workbook2").Cells(1, 1).PasteSpecial Paste:=xlPasteAll

I've tried adding the following line to rename the table back to "Table1" after pasting, but it didn't work.

Sheets("workbook2").ListObjects(1).Name = "Table1"

My formulas are like this:
=sumifs(Table1[people],Table1[Region],"",Table1[County],"")

If anyone has any suggestions or tips it would be much appreciated!
Thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
There are several ways to go about this. If it were me, I would change your code so that you weren't copying Table1 from workbook1, just the data. Use currentregion or something like that to copy the actual data into workbook2 without copying it over as a valid table. If you need more help on how to select the data you want to copy let me know.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top