View Single Post
  #6   Report Post  
Dana DeLouis
 
Posts: n/a
Default

This is not the perfect solution, and won't work with a formula like
=Sum(A1:A5), but maybe there are some ideas he
This demo looks at C1, and puts an adjusted formula in D1.
Again, this is not complete, but may give you some ideas:

Sub Demo()
Dim S As String
Dim rngArea
Dim Cell

'// Set up
[A1] = 2
[A2].Formula = "=8/2"
[A4].Formula = "=Pi()"
[A5].Formula = "=Sin(Pi()/4)"

[C1].Formula = "=A1+$A$2+A$4+$A5"

S = Range("C1").Formula

For Each rngArea In Range("C1").DirectPrecedents.Areas
For Each Cell In rngArea.Cells
S = Replace(S, Cell.Address(True, True), Cell.Value)
S = Replace(S, Cell.Address(True, False), Cell.Value)
S = Replace(S, Cell.Address(False, True), Cell.Value)
S = Replace(S, Cell.Address(False, False), Cell.Value)
Next Cell
Next rngArea
Range("D1").Formula = S
End Sub


--
Dana DeLouis
Win XP & Office 2003


"robert.bone" wrote in message
...
Thanks again. That was useful and in the meantime I'd been testing out
something similar - but now the tricky bit. Or maybe not so difficult -
I'm
not sure! The next step would be to replace the cell references shown in
the formula with the actual values. I came across a routine to do that
but
it made calls to functions that are not within VBA, so is of limited use
to
most people using standard Excel

I thought one approach would be to parse the formula string and look for
cell references then use INDIRECT to return with the cell value. In
pseudo
code it might look like this:

flen = len(formula_in) ; get character count of
formula
expression
for I = 1 to flen
nextchar = mid(formula_in, I, 1) ; take one character at a time
for J = 1 to delim ; delim is number of
delimiters
; delim is a string array holding delimiter characters such as ( )
+ -
* / ^ &
if nextchar = delim(J) then
; just copy and add character on to end of output string
formula_out = formula_out & nextchar
J = delim ; ugly, but crash out of
loop
else
; otherwise build up a text item that may or may not be a cell
reference
nextitem = nextitem & nextchar
J = delim
endif
next J

etc, etc
This is getting messy, but the idea is that the next cell references or
function name will be built up in nextitem.
Thereafter, nextitem can then be checked to see if it is a cell reference
ISREF() and if so INDIRECT() would be used to get the value, TEXT() would
convert it to a string and it could be added onto the end of formula_out.
If
nextitem turns out not to be a cell reference eg say it is SQRT then it is
simply concatenated onto formula_out.

I'm afraid my programming skills are rather rusty and VBA was never my
strong point, by if anyone out there would like to develop something along
the above lines I (and judging from discussion grps) many others would be
delighted. In fact I think this should be a standards Excel function,
but....



"Bernie Deitrick" wrote:

Then you need to use a function. Copy the code below into a module in
your
workbook, and use it like

=ShowFormula(A1)

HTH,
Bernie
MS Excel MVP

Function ShowFormula(inCell As Range) As String
ShowFormula = Mid(inCell.Formula, 2, _
Len(inCell.Formula)) & " = " & inCell.Value
End Function



"BoneR" wrote in message
...
Thanks Bernie, I have used string concatination in the past but when
the
expressions get long and complex it becomes very tedious. What I had
in

mind
was a more general function that actually looked at the cell containing

the
maths expression directly.

"Bernie Deitrick" wrote:

To show addition of the values in A1 and A2, use the formula

= A1 & " + " & A2 & " = " & A1 + A2

Of course, you might want to have a check that A1 and A2 are filled,
but

you
get the idea.

HTH,
Bernie
MS Excel MVP

"BoneR" wrote in message
...
In Excel I would like to display the values that go into a
formula -

in
addition to the answer itself. This is to mimic the way hand

calculations
would be set out on paper. In other words I'd like a function that

takes
the
maths expression within a cell and returns with a string where the

cell
references have been replaced with actual values.