Using VBA Replace Function to Edit Formulas

smit3446

New Member
Joined
Nov 16, 2015
Messages
44
I'm trying to create a loop to Find and Replace parts of formulas on my sheet labeled Output. With my current code, the replace function isn't working and I suspect it's trying to replace string functions only. I need this to alter the formulas. For example here is my array on Sheet1 with column A consisting of old values and a column B consisting of the new values I would like to have in place of column A:

Calc 1999Calc CY(-18)
Calc 2000Calc CY(-17)
Calc 2001Calc CY(-16)
Calc 2002Calc CY(-15)
Calc 2003Calc CY(-14)
Calc 2004Calc CY(-13)
Calc 2005Calc CY(-12)
Calc 2006Calc CY(-11)
Calc 2007Calc CY(-10)
Calc 2008Calc CY(-9)
Calc 2009Calc CY(-8)
Calc 2010Calc CY(-7)
Calc 2011Calc CY(-6)
Calc 2012Calc CY(-5)
Calc 2013Calc CY(-4)
Calc 2014Calc CY(-3)
Calc 2015Calc CY(-2)
Calc 2016Calc CY(-1)
Calc 2017Calc CY(0)

<tbody>
</tbody>


And here is my code:

Code:
Sub FindReplaceAll()

Dim sht As Worksheet
Dim fnd As Variant
Dim rplc As Variant
Dim i As Integer


i = 1


Do While i < 20


fnd = Sheets("Sheet1").Range("A" & i).Value
rplc = Sheets("Sheet1").Range("B" & i).Value


'MsgBox fnd
'MsgBox rplc


  Set sht = Sheets("Output")


   sht.Cells.Replace what:=fnd, Replacement:=rplc, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False


i = i + 1
Loop


End Sub

Any help to get this working to replace text within the formula on the Outputs tab would be very appreciated. Thank you!
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try changing
Code:
LookAt:=xlWhole
to
Code:
LookAt:=xlPart
 
Upvote 0

Forum statistics

Threads
1,214,382
Messages
6,119,194
Members
448,874
Latest member
Lancelots

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