Display formulae as values of each cell reference

P

plotzmoney

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 ha
various formulas and cell references in it) and convert those formula
and references into their values within the formula bar into a separat
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 highligh
all of them)
2. I would run the macro

Macro Output

1. The macro would paste a formula into the corresponding cells directl
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 th
following formula: =(50+200+250)/10
 
A

Auric__

plotzmoney said:
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
 
R

Ron Rosenfeld

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.
 
P

plotzmoney

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



'Ron Rosenfeld[_2_ said:
;1602223']On Tue, 29 May 2012 17:23:23 +0000, plotzmone
Hello all, I was wondering if someone could please help me with 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 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 cell 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 fro
other contents. This becomes even more complicated if you also want t
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 tha
might be within the cell
 
R

Ron Rosenfeld

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
=================================================
 
R

Ron Rosenfeld

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
====================================
 
P

plotzmoney

Thanks so much Ron!!! Works great

'Ron Rosenfeld[_2_ said:
;1602260']On Wed, 30 May 2012 15:43:00 +0000, plotzmone
Ron, thank you for the response. I was looking for what you wer 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 specifie
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
===================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top