Find and replace formulas with vba

nlundbyek

New Member
Joined
Jun 12, 2018
Messages
4
I am trying to use find and replace to replace one formula with another. When I use my code with words in the string it works:

Sub new()


Cells.Replace What:="Hej", _
Replacement:="Farvel", LookAt:=xlWhole, SearchOrder:= _
xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False


End Sub

But when I use it with formulas in the string it won't work:

Sub new()


Cells.Replace What:="=LOPSLAG(C$4;'Kalender skjult'!$A$1:$H$5000;7;FALSK)", _
Replacement:="=HVIS($C$18>0;1;0)", LookAt:=xlWhole, SearchOrder:= _
xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False


End Sub

It's in Danish. Hope someone can tell me why it won't work with formulas :-)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Not a strong area of mine & unable to test, but could the problem be that vba wants comma separators not the regional separator in the formulas in one or both places?
 
Upvote 0
@Peter_SSs,

yep, that could be true.
I've taken a quick look through my VBA files and if i insert formulas in VBA, i'm using commas instead semicolon regardsless of my listseperator usual is a semicolon and subsequently I'm using RC style instead of "A1" to create the formula.
 
Upvote 0
yep, that could be true.
I've taken a quick look through my VBA files and if i insert formulas in VBA, i'm using commas instead semicolon regardsless of my listseperator usual is a semicolon and subsequently I'm using RC style instead of "A1" to create the formula.
So do you know if the OP should try swapping semicolon for comma in both
What:="=LOPSLAG(C$4;'Kalender skjult'!$A$1:$H$5000;7;FALSK)"
Replacement:="=HVIS($C$18>0;1;0)"
or just the Replacement?
 
Upvote 0
@Peter_SSs,

The OP should change the semicolon in FIND and REPLACE. RC style is not mandatory so could both be used.
Next to that, The OP needs to change the name of the VBA from New() to something else meaningful because New is a restricted word with specific usage within VBA.
 
Last edited:
Upvote 0
Another point, is that if the formula has been dragged to the right this part =LOPSLAG(C$4 will change to =LOPSLAG(D$4 etc. So only one cell will be replaced.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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