Display Evaluated formula of another cell

Mat

Well-known Member
Joined
Sep 17, 2003
Messages
509
Hi,

I'm pretty sure there's a way to do this in VBA.

I would like to show the evaluated formula (with the numbers, not the cell reference) in a cell, based on another cell formula.

Let say I have :
A2 = 3
B3 = 4
D3 = 5
A1 = "=A2+B3*D3"

Then B2 = "=EvaluatedFormula(A1") would display : "=2+4*5"

So I need a routine that will replace the addresses by their actual value in a formula.

B2 = "=SUBSTITUTEADDRESSBYVALUE(FORMLATEXT(B2))

Thank you

Mathieu
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I found a code to extract cell reference from a cell, so I end up with that code, that does the job for now, but it's not bulletproof. It doesn't work with named range which would be required.

VBA Code:
Function DisplayFormula(Rg As Range) As String
'Updateby Extendoffice
    Dim xRetList As Object
    Dim xRegEx As Object
    Dim I As Long
    Dim xRet As String
    Application.Volatile
    Set xRegEx = CreateObject("VBSCRIPT.REGEXP")
    With xRegEx
        .Pattern = "('?[a-zA-Z0-9\s\[\]\.]{1,99})?'?!?\$?[A-Z]{1,3}\$?[0-9]{1,7}:)\$?[A-Z]{1,3}\$?[0-9]{1,7})?"
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
    End With
    Set xRetList = xRegEx.Execute(Rg.Formula)
    If xRetList.Count > 0 Then
        For I = 0 To xRetList.Count - 1
            xRet = xRet & xRetList.Item(I) & ";"
        Next
        DisplayFormula = Left(xRet, Len(xRet) - 2)
    Else
        DisplayFormula = "No Matches"
    End If
   
    Dim formul As String
    formul = Rg.Formula
    Dim tmp As Variant
    tmp = Split(DisplayFormula, ";")
    For I = 0 To UBound(tmp)
        formul = Replace(formul, tmp(I), Format(Range(tmp(I)).Value, Range(tmp(I)).NumberFormat))
       
    Next I
    DisplayFormula = Right(formul, Len(formul) - 1)
    DisplayFormula = Replace(DisplayFormula, "*", " · ")
    DisplayFormula = Replace(DisplayFormula, "-", " - ")
    DisplayFormula = Replace(DisplayFormula, "+", " + ")
    DisplayFormula = Replace(DisplayFormula, "/", " / ")
End Function
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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