Formulas/VBA: .Replace after .Copy also edits source formulas

Will_B

Board Regular
Joined
Mar 4, 2009
Messages
79
Hi. My problem concerns copying and pasting formulas using VBA, then using .Replace to edit the formula in the destination cells. The problem I have is that the .Replace also changes the formula in the source cells. I want only the formulas in the destination cells to be edited. The code below is a simplified version of what I want to do, but it produces the same error. I am using Excel 2007.

To replicate this, put these formulas in cells A1 and A2 of Sheet1:
=1+2
=2+2
... then run the code below. You'll see that the original formulas on Sheet1 are also edited.

How do I keep the formulas on Sheet1 from being edited?
-------------------------------------------------------
Sub Test()
Dim wksCopyFrom As Worksheet
Dim wksDestination As Worksheet
Dim rngCopyFrom As Range
Dim rngDestination As Range

'Worksheet that has the formula I want to copy...
Set wksCopyFrom = Worksheets("Sheet1")
'Where the formula (originally) is "=1+2"...
Set rngCopyFrom = wksCopyFrom.Range("A1:A2")
'Worksheet where I want to copy the formula
Set wksDestination = Worksheets("Sheet2")
'Where I want to paste the formula, then edit it with find-and-replace...
Set rngDestination = wksDestination.Range("A1:A2")

'Copy the formula
rngCopyFrom.Copy
'Paste it
rngDestination.PasteSpecial
'Turn off copy mode
Application.CutCopyMode = False

'In the destination cells, CHANGE "2" to be "9" in the formulas
rngDestination.Cells.Replace _
What:=2, Replacement:=9, _
SearchOrder:=xlByColumns

End Sub
-------------------------------------------------
NOTE: In my real-world situation, what I am copying are various large tables that vary in size and include formulas, fixed text, conditional formatting, borders, etc. I have to copy it N times and edit each instance to refer to a different worksheet (of which there are N many). Therefore my final code must capture all text, formatting, etc., to be copied, too. This is why I haven't tried to simply copy the formulas as text. Hopefully my example above does not oversimplify the situation. Thanks for considering my problem!!!!
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi Will

I just tried your code and it works ok. The source cells did not change. Can you try it following the instructions you posted?
 
Upvote 0
It works for me this morning, too. Sheesh. Yesterday I tested it three times before pasting the code into this posting. Each time the problem occured. Apparently it was a JOOTT because my more complex code I'm working on also works fine this morning. Perhaps this is a bug in Excel 2007, that it needs a night's rest to run VBA? I swear it wasn't programmer fatigue. :biggrin: Thanks for taking the time to run this and to respond so quickly. If nothing else, this posting will let others know it is just a temporary JOOTT.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,375
Members
448,888
Latest member
Arle8907

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