VBA copy formulas without the clipboard and get updated relative references?

SoftDad

New Member
Joined
Sep 29, 2016
Messages
2
OK, I've got it that I can directly copy a row of formulas without using Select/Copy/Paste -
With range1 and range2 each set to a row:
Rich (BB code):
 range2.formula = range1.formula
This is clearly more efficient and cleaner that using the clipboard.
Problem is, the formulas are copied verbatim, with relative references not getting updated. I.e. if I have
Rich (BB code):
A1 is 2 
B1 is =A1 * A1
and I use the direct copy method to copy this to row 2, I get
Rich (BB code):
A2 is 2
B2 is =A1 * A1
I would expect this to behave as when I copy/paste, i.e I would expect
Rich (BB code):
B2 to be =A2 * A2
Is there a way to get relative address updating in a direct copy? The best I've come up with is to modify my formulas using
INDEX(Sht, ROW(), COLUMN())
to refer to the cell itself (where Sht is a range referring to the whole sheet). Then I use OFFSET to get relative references. My B1 now becomes:
Rich (BB code):
=OFFSET(INDEX(Sht, ROW(), COLUMN()),0,-1) * OFFSET(INDEX(Sht, ROW(), COLUMN()),0,-1)
And when I use my direct copy I get the expected result. But for any by the simplest of formulas this becomes really messy.

The other disadvantage is that if I decide to insert a new column or move my columns around I have to update all my offsets manually.

There must be a better way! Anybody, please! :confused:
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Have you tried

Code:
Range1.Copy Range2

This does use the clipboard, but the dynamic references are automatically changed.
 
Last edited:
Upvote 0
Have you tried

Code:
Range1.Copy Range2

This does use the clipboard, but the dynamic references are automatically changed.
Well hey, I tried your tip and it just works. And Lo! - it doesn't seem to leave junk in the clipboard (which is what has concerned me). It seems to clear the clipboard.
Thanks for that tip, but boy it sure makes me feel dumb after all that formula finagling. :eek:
 
Upvote 0
You're welcome. And I can't count how many times I've done things the long way, only to find an easier way later! :confused:
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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