J
joeu2004
Dana (and other VBA experts),
In the thread "Expanding Formulas", you offered the macro below as a
(great!) starting point. I made some minor modifications.
The Replace() operation seems to make the appropriate substitution
only for relative references of the form B1. For example, absolute
references of the form $B$1 are not replaced.
The root cause of the problem seems to be that p.Address(false,false)
always returns a relative reference, even if the original reference is
absolute. I presume that the Replace() operation is ineffective
because "B1" does not match "$B$1" in the formula.
Changing things to p.Address(true,true) rectifies that; but of course,
then relative references are not substituted.
Can the macro be changed easily to substitute all forms of references?
The modified macro:
Sub Demo()
Dim s, p, a, v
Dim cell
Debug.Print "-----------"
For Each cell In Selection
s = cell.Formula
Debug.Print s
On Error Resume Next
p = cell.Precedents
If Err = 0 Then
For Each p In cell.Precedents
a = p.Address(False, False)
v = p.Value
s = Replace(s, a, v)
Next p
End If
Debug.Print s
Debug.Print Evaluate(s)
Range("Sheet2!" & cell.Address).Formula = s
Next cell
End Sub
In the thread "Expanding Formulas", you offered the macro below as a
(great!) starting point. I made some minor modifications.
The Replace() operation seems to make the appropriate substitution
only for relative references of the form B1. For example, absolute
references of the form $B$1 are not replaced.
The root cause of the problem seems to be that p.Address(false,false)
always returns a relative reference, even if the original reference is
absolute. I presume that the Replace() operation is ineffective
because "B1" does not match "$B$1" in the formula.
Changing things to p.Address(true,true) rectifies that; but of course,
then relative references are not substituted.
Can the macro be changed easily to substitute all forms of references?
The modified macro:
Sub Demo()
Dim s, p, a, v
Dim cell
Debug.Print "-----------"
For Each cell In Selection
s = cell.Formula
Debug.Print s
On Error Resume Next
p = cell.Precedents
If Err = 0 Then
For Each p In cell.Precedents
a = p.Address(False, False)
v = p.Value
s = Replace(s, a, v)
Next p
End If
Debug.Print s
Debug.Print Evaluate(s)
Range("Sheet2!" & cell.Address).Formula = s
Next cell
End Sub