ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Display formulae as values of each cell reference (https://www.excelbanter.com/excel-programming/446186-display-formulae-values-each-cell-reference.html)

plotzmoney

Display formulae as values of each cell reference
 
Hello all, I was wondering if someone could please help me with a macro. Any and all help is appreciated:

Essentially, it would be a macro where you take a cell (which has various formulas and cell references in it) and convert those formulas and references into their values within the formula bar into a separate cell next to it.

For example:

Where Cell A1 = Cells (F2+F3+F4)/F5
F2 = 50
F3= 200
F4= 250
F5=100

Steps for Macro

1. I would highlight cell A1 (or if multiple cells, I would highlight all of them)
2. I would run the macro

Macro Output

1. The macro would paste a formula into the corresponding cells directly to the right of it. (In this case, it would be cell B1)
2. In cell B1, the cell would show a value of "5", and would show the following formula: =(50+200+250)/100

Auric__

Display formulae as values of each cell reference
 
plotzmoney wrote:

Hello all, I was wondering if someone could please help me with a macro.
Any and all help is appreciated:

Essentially, it would be a macro where you take a cell (which has
various formulas and cell references in it) and convert those formulas
and references into their values within the formula bar into a separate
cell next to it.

For example:

Where Cell A1 = Cells (F2+F3+F4)/F5
F2 = 50
F3= 200
F4= 250
F5=100

Steps for Macro

1. I would highlight cell A1 (or if multiple cells, I would highlight
all of them)
2. I would run the macro

Macro Output

1. The macro would paste a formula into the corresponding cells directly
to the right of it. (In this case, it would be cell B1)
2. In cell B1, the cell would show a value of "5", and would show the
following formula: =(50+200+250)/100


As it happens, I solved a similar problem for myself some time ago. Try this:

Sub SelectionFormula2Value()
Dim cell As Range
For Each cell In Selection.Cells
cell.Offset(0, 1).Formula = "'" & cell.Formula
Next
End Sub

--
Since all the ideas involve you, I'm not suprised.

Ron Rosenfeld[_2_]

Display formulae as values of each cell reference
 
On Tue, 29 May 2012 17:23:23 +0000, plotzmoney wrote:


Hello all, I was wondering if someone could please help me with a macro.
Any and all help is appreciated:

Essentially, it would be a macro where you take a cell (which has
various formulas and cell references in it) and convert those formulas
and references into their values within the formula bar into a separate
cell next to it.

For example:

Where Cell A1 = Cells (F2+F3+F4)/F5
F2 = 50
F3= 200
F4= 250
F5=100

Steps for Macro

1. I would highlight cell A1 (or if multiple cells, I would highlight
all of them)
2. I would run the macro

Macro Output

1. The macro would paste a formula into the corresponding cells directly
to the right of it. (In this case, it would be cell B1)
2. In cell B1, the cell would show a value of "5", and would show the
following formula: =(50+200+250)/100


It is simple to show the formula itself with the range.formula method.

To replace the cell references with their values becomes more complex, as you need to write a routine that differentiates cell references from other contents. This becomes even more complicated if you also want to include NAMEs.

Furthermore, you write you also want to convert "formulas" to values. Does this refer to the entire formula? or also to various formulas that might be within the cell.

plotzmoney

Ron, thank you for the response. I was looking for what you were eluding to in your last question. Where all formulas, even within referenced cells, would be converted to values. Again, thanks for chiming in and for the help.



Quote:

Originally Posted by Ron Rosenfeld[_2_] (Post 1602223)
On Tue, 29 May 2012 17:23:23 +0000, plotzmoney wrote:


Hello all, I was wondering if someone could please help me with a macro.
Any and all help is appreciated:

Essentially, it would be a macro where you take a cell (which has
various formulas and cell references in it) and convert those formulas
and references into their values within the formula bar into a separate
cell next to it.

For example:

Where Cell A1 = Cells (F2+F3+F4)/F5
F2 = 50
F3= 200
F4= 250
F5=100

Steps for Macro

1. I would highlight cell A1 (or if multiple cells, I would highlight
all of them)
2. I would run the macro

Macro Output

1. The macro would paste a formula into the corresponding cells directly
to the right of it. (In this case, it would be cell B1)
2. In cell B1, the cell would show a value of "5", and would show the
following formula: =(50+200+250)/100


It is simple to show the formula itself with the range.formula method.

To replace the cell references with their values becomes more complex, as you need to write a routine that differentiates cell references from other contents. This becomes even more complicated if you also want to include NAMEs.

Furthermore, you write you also want to convert "formulas" to values. Does this refer to the entire formula? or also to various formulas that might be within the cell.


Ron Rosenfeld[_2_]

Display formulae as values of each cell reference
 
On Wed, 30 May 2012 15:43:00 +0000, plotzmoney wrote:


Ron, thank you for the response. I was looking for what you were eluding
to in your last question. Where all formulas, even within referenced
cells, would be converted to values. Again, thanks for chiming in and
for the help.



This User Defined Function can do some of what you want. It will only convert cell references to the values within those cells. I believe it will retain the format of those cells. Multicell range references will viewed as an array of values. Perhaps it will do what you require.

The long string of .pattern is a regular expression that should be able to recognize cell references of the A1 system. Ensure it is all on one line as the NG display may word wrap inappropriately.

I can't do much tweaking, but it should work for simple formulas. Anything more complex will require a formula parser, which is beyond what I can provide for you.


To enter this User Defined Function (UDF), <alt-F11 opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

=ConvertFormula(A1)

in some cell.

========================================
Option Explicit
Function ConvertFormula(r As Range) As String
Dim re As Object, mc As Object, m As Object
Dim re2 As Object, sRepl As String
Dim s As String, v As Variant
Dim i As Long
Dim c As Range
s = r.Formula

Set re = CreateObject("vbscript.regexp")
Set re2 = CreateObject("vbscript.regexp")

With re
.Global = True
.Pattern = "\$?\b(?:XF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b([:\s]\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b)?"
End With

If re.Test(s) = True Then
Set mc = re.Execute(s)
For Each m In mc
If InStr(m, ":") 0 Then
ReDim v(1 To Range(m).Count)
For i = 1 To UBound(v)
v(i) = Range(m)(i)
Next i
sRepl = "{" & Join(v, ", ") & "}"
Else
sRepl = Range(m).Text
End If
With re2
.Global = True
.Pattern = "\b" & m & "\b"
s = .Replace(s, sRepl)
End With
Next m
End If

ConvertFormula = s

End Function
=================================================

Ron Rosenfeld[_2_]

Display formulae as values of each cell reference
 
On Wed, 30 May 2012 15:43:00 +0000, plotzmoney wrote:

Ron, thank you for the response. I was looking for what you were eluding
to in your last question. Where all formulas, even within referenced
cells, would be converted to values. Again, thanks for chiming in and
for the help.


To have this run as a macro, on the range you select, as you specified in your original post, try this macro instead:

===================================
Option Explicit
Sub ConvertFormula()
Dim re As Object, mc As Object, m As Object
Dim re2 As Object, sRepl As String
Dim s As String, v As Variant
Dim i As Long
Dim c As Range, r As Range

For Each r In Selection

s = r.Formula

Set re = CreateObject("vbscript.regexp")
Set re2 = CreateObject("vbscript.regexp")

With re
.Global = True
.Pattern = "\$?\b(?:XF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b([:\s]\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b)?"
End With

If re.Test(s) = True Then
Set mc = re.Execute(s)
For Each m In mc
If InStr(m, ":") 0 Then
ReDim v(1 To Range(m).Count)
For i = 1 To UBound(v)
v(i) = Range(m)(i)
Next i
sRepl = "{" & Join(v, ", ") & "}"
Else
sRepl = Range(m).Text
End If
With re2
.Global = True
.Pattern = "\b" & m & "\b"
s = .Replace(s, sRepl)
End With
Next m
End If

r.Offset(columnoffset:=1).Value = r.Text & s
Next r
End Sub
====================================

plotzmoney

Thanks so much Ron!!! Works great

Quote:

Originally Posted by Ron Rosenfeld[_2_] (Post 1602260)
On Wed, 30 May 2012 15:43:00 +0000, plotzmoney wrote:

Ron, thank you for the response. I was looking for what you were eluding
to in your last question. Where all formulas, even within referenced
cells, would be converted to values. Again, thanks for chiming in and
for the help.


To have this run as a macro, on the range you select, as you specified in your original post, try this macro instead:

===================================
Option Explicit
Sub ConvertFormula()
Dim re As Object, mc As Object, m As Object
Dim re2 As Object, sRepl As String
Dim s As String, v As Variant
Dim i As Long
Dim c As Range, r As Range

For Each r In Selection

s = r.Formula

Set re = CreateObject("vbscript.regexp")
Set re2 = CreateObject("vbscript.regexp")

With re
.Global = True
.Pattern = "\$?\b(?:XF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b([:\s]\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b)?"
End With

If re.Test(s) = True Then
Set mc = re.Execute(s)
For Each m In mc
If InStr(m, ":") 0 Then
ReDim v(1 To Range(m).Count)
For i = 1 To UBound(v)
v(i) = Range(m)(i)
Next i
sRepl = "{" & Join(v, ", ") & "}"
Else
sRepl = Range(m).Text
End If
With re2
.Global = True
.Pattern = "\b" & m & "\b"
s = .Replace(s, sRepl)
End With
Next m
End If

r.Offset(columnoffset:=1).Value = r.Text & s
Next r
End Sub
====================================


Ron Rosenfeld[_2_]

Display formulae as values of each cell reference
 
On Thu, 31 May 2012 15:19:39 +0000, plotzmoney wrote:

Thanks so much Ron!!! Works great


Glad to help. Thanks for the feedback.

Lepista

This is great. Is there any way to keep the formatting of the referenced cells?

eg:
A1; 2.11237864872634 (formatted to 2 DP, for example)
A2: 3.141592654 (formatted to 2 DP, for example)
A3: = A1 + A2
ConvertFormula(A3) shows "2.11237864872634 + 3.15297459"

Instead I would like "2.11 + 3.14"

Any help would be greatly appreciated!


All times are GMT +1. The time now is 01:04 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com