"PasteSpecial xlPasteFormulas" with relative reference with destination ListObject

bmont

New Member
Joined
May 28, 2013
Messages
14
Office Version
  1. 2016
Platform
  1. MacOS
My goal is to copy-paste formulas from tableSource (a ListObject) to tableDest (also a ListObject) and to have those pasted formulas refer to tableDest, not to tableSource, which is what I get when I do a little something like this:

ListObjSource.ListRows(1).Range.Copy
rangeDestination.PasteSpecial xlPasteFormula


...resulting in formulas within tableDest referring to not their native ListObject.

One could use Replace to edit the strings but is there a smarter way?

Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi

When you post code please post the declarations and initialisations of the objects mentioned.
In this case it would also help to post the structure of the tables and the logic of what you're doing.

I'll assume that
. rangeDestination is a range inside a second table
. this second table has fields with the same name as the first table so that the same formula can work on both.

With these assumptions the solution is to copy the formula directly.

This is an example that copies the formula in the second column of the first table into the second column of the second table.

Test:

VBA Code:
Sub Macro1()
Dim ListObjSource As ListObject, ListObjDest As ListObject

Set ListObjSource = ActiveSheet.ListObjects(1)
Set ListObjDest = ActiveSheet.ListObjects(2)

' copy the formula on the second column
ListObjDest.ListRows(1).Range(2).Formula = ListObjSource.ListRows(1).Range(2).Formula

End Sub
 
Upvote 0
Solution
Thank you, pgc01. This works perfectly and has me thinking differently now about copy-paste vs your approach. Awesome.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,665
Members
449,462
Latest member
Chislobog

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