Need to replace a formula while keeping some of the contents in the formula _ VBA!

luismemo21

New Member
Joined
Mar 2, 2018
Messages
1
Hello All,

First post on this forum. Just to be clear, I have already searched similar threads (here and and elsewhere) and tried to piece together a VBA code that will work, but I just don't have the adequate experience under my belt to write a executable code.

I have a maybe a thousand cells across several hundred worksheets that contain formulas in the following format:

=(max(G11,H11)-G15+S15+O11)

And I would like to change the formula to:

=G11-G15-S15+O11

The formulas are all along a single column, lets say Column "S".

Using Find>Replace would work if I was uniformly changing the equation across all cells. But each formula is unique and I have to keep the specific references for that formula.

My logic is as follows:
  1. For each cell in the chosen range (in this case, a range in column S), check to see if the formula starts with "=(max("
  2. If it does, then place the references I need in variables (e.g. var1 = G11, var2 = G15, etc etc). Right now I am using the "Mid" function to extract those references from the old equation.
  3. Replace the current formula with a new formula (e.g. =var1 - var2 - var3 + var4)

This is my code so far, but I am still fairly new to VBA so I'm sure there's a lot wrong here
Code:
Function CellFormula(Rng As Range) As String
    CellFormula = Rng.Formula
End Function

Sub EnrgyEqFix()

'equation format that I wish to be changed:
'=(max(G11,H11)-G15+S15+O11)
'equation format that I wish to have at the end:
'=G11-G15+S15+O11
'specific cell refrences must be kept for each formula

Dim ds_elv As String     'downstream elevation reference i.e. first "G" reference
Dim us_elv As String     'Upstream elevation reference i.e. second "G" reference
Dim us_energy As String  'upstream cumulative energy loss i.e. "S" refrence
Dim h_loss As String     'friction loss thru pipe i.e. "O" refrence
Dim ERng As Range        'range of cells that contains Energy Formula
Dim Ecell As Range       'cell in ERng
Dim ToRep As String      'condition to see if formula should be replaced
Dim NewEq As String      'new equation I wish to have


Set ERng = Range("S11:S300")
'range of cells that possibly have old equation
ToRep = "=(Max("
'this is the string to look for and decide if a change should be made
NewEq = "=ds_elv-us_elv-us_energy+h_loss"
'this is the end equation I want to have

For Each Ecell In ERng.Cells

    If Left(CellFormula(Ecell), 4) = ToRep Then
        ds_elv = Mid(CellFormula(Ecell), 7, 3)
        us_elv = Mid(CellFormula(Ecell), 16, 3)
        us_energy = Mid(CellFormula(Ecell), 20, 3)
        h_loss = Mid(CellFormula(Ecell), 24, 3)
        
        ActiveCell.Value = NewEq
    End If

Next Ecell

End Sub


Any advice or pointers would be much appreciated. Thank you all!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,224,905
Messages
6,181,663
Members
453,059
Latest member
jkevin

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