Using VBA search and replace in formulas

Foodo

New Member
Joined
Dec 22, 2015
Messages
20
Hi Excel Experts,

I am fairly new to VB/Macro so any help would be much appreciated. I am trying to create a macro that will search through a worksheet for a particular portion of a formula and replace with something else. Below is what I have built so far:



For Each cell In Selection.SpecialCells(xlConstants, xlTextValues)

Sub ReplaceFormula()

Dim cell As Range
For Each cell In Selection.SpecialCells(xlConstants, xlTextValues)




Cells.Find(What:="OLD", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Replace What:="OLD", Replacement:="NEW", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.FindNext(After:=ActiveCell).Activate


Next cell
End Sub


It seems to work if "OLD" is in text, but if it is in a formula (e.g., "=OLD"), it will ignore. Help please?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
MAybe this
Rich (BB code):
Sub MM1()
    Dim Findtext As String, Replacetext As String
    Findtext = "old"
    Replacetext = "new"
    Selection.Replace what:=Findtext, replacement:=Replacetext, lookat:=xlPart, MatchCase:=True

End Sub
But rather than select why not use the column to be the the range
Rich (BB code):
Sub MM1()
    Dim Findtext As String, Replacetext As String
    Findtext = "old"
    Replacetext = "new"
     Columns("A").Replace what:=Findtext, replacement:=Replacetext, lookat:=xlPart, MatchCase:=True

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,523
Messages
6,131,151
Members
449,626
Latest member
Stormythebandit

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