Simple find & replace question

RonOliver

Board Regular
Joined
Aug 30, 2022
Messages
97
Office Version
  1. 365
Platform
  1. Windows
Hi everyone!

I'm looking for a way to append a formula to another existing formula, both with a prefix and a suffix.

For example, if I have a bunch of cells with =SUM(*,*), with the asterisks being placeholders for other cells that are different every time, and I want to put them into =IF(SUM(*,*),1,0), how can I replace them all in one go? My original formula is VERY long and I wouldn't want to post it here, but this is basically what it boils down to. It should be really easy, but it's taking me a lot to do this.

Thanks!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If you are happy to use a macro this might work.
Note: please try it on a copy of your workbook

VBA Code:
Sub updateformula()

    Dim rng As Range, arr As Variant
    Dim i As Long, j As Long
    
    Set rng = ActiveSheet.UsedRange
    arr = rng.Formula2
    
    For i = 1 To UBound(arr)
        For j = 1 To UBound(arr, 2)
            If Left(arr(i, j), 4) = "=SUM" Then
                arr(i, j) = "=IF(" & Mid(arr(i, j), 2) & ",1,0)"
            End If
        Next j
    Next i
    
    rng.Formula2 = arr

End Sub
 
Upvote 1
Manual option:
Replace = with zz
Replace SUM with IF(SUM
Replace ) with ),1,0)
Replace zz with =
 
Upvote 0
Solution
If you are happy to use a macro this might work.
Note: please try it on a copy of your workbook

VBA Code:
Sub updateformula()

    Dim rng As Range, arr As Variant
    Dim i As Long, j As Long
  
    Set rng = ActiveSheet.UsedRange
    arr = rng.Formula2
  
    For i = 1 To UBound(arr)
        For j = 1 To UBound(arr, 2)
            If Left(arr(i, j), 4) = "=SUM" Then
                arr(i, j) = "=IF(" & Mid(arr(i, j), 2) & ",1,0)"
            End If
        Next j
    Next i
  
    rng.Formula2 = arr

End Sub
Will try this shortly. I need to start learning VBA already. It looks like it's the only way to go when problems are a little too big.
 
Upvote 0
Manual option:
Replace = with zz
Replace SUM with IF(SUM
Replace ) with ),1,0)
Replace zz with =
I love this! Didn't even think of replacing the equal sign. Will check and let you know if it worked for me! :)
 
Upvote 0
Manual option:
Replace = with zz
Replace SUM with IF(SUM
Replace ) with ),1,0)
Replace zz with =
I thought of that but the wording "My original formula is VERY long" implied there might be more than one ")" in the formula.
 
Upvote 0
I interpreted that as just meaning there were a lot of individual cell references in there. If not, then this will create some interesting formulas... ;)
 
Upvote 0
I thought of that but the wording "My original formula is VERY long" implied there might be more than one ")" in the formula.
There are, but I found a non-repeating pattern within the formula that I can use. I just kept coming across the equal sign error and I didn't know how to work around it.
 
Upvote 0
Thanks very much to both of you. You've saved me hours of work. :)
 
Upvote 0
Glad Rory's Find & Replace worked for you. Perhaps you can mark his post #3 as the solution 😉
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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