Replace part of a formula?

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,946
Office Version
  1. 2013
Platform
  1. Windows
Hi

I am trying to modify a cell formula with the Replace function.
I can't see what I am doing wrong. Anyone help?

Code:
The Formula in the target cell = 
='C:\Documents and Settings\Holiday macro\[Holiday2011.xls]Year 2011'!C5
 
I am using the below code but it returns just the end portion ='Year 2012'!C5
 
where I expect just the year to change
='C:\Documents and Settings\Holiday macro\[Holiday2012.xls]Year 2012'!C5
 
Where last year = 2011
Yr=2012
 
    Range("C3").Formula = Replace(Range("C3").Formula, LastYr, Yr)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If LastYr equal 2011 and Yr equals 2012, then the code line you posted works for me. Perhaps some of the surrounding code is doing something unintended... why don't you post your actual code (copy/paste it please, don't retype it... it's hard to see past your typos) and let us take a look.
 
Upvote 0
Thanks for the reply:

With this code;

Code:
Yr = 2012
    ActiveSheet.Name = "Year " & Yr
    LastYr = Yr - 1
    Range("C3").Formula = Replace(Range("C3").Formula, LastYr, Yr)
 
and this formula in C3
='C:\Documents and Settings\Holiday macro\[Holiday2011.xls]Year 2011'!C5
 
I get
 
='Year 2012'!C5
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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