A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Again an XClent and superb collection of valid arguments. However, the main goal has remained to satisfy my boss who is a formula hater specially while the same have references. I think the current code would convert the formulas to work in the same fashion as had been the original ones with references, otherwise would result into a #Name Error in case of a string.
But sure would love to hear from u in respect of displayed values as well. Might come in handy anytime.
Okay, here is a new modification of my code to which I added an optional second argument which controls whether the function returns the actual value in the cell or the formatted value for the cell. The optional argument is a Boolean which defaults to False, meaning display the actual value in the cell (True means display the formatted value for the cell). I took the liberty to change how and when quote marks are displayed. When the optional argument is False (display actual values), Text is quoted and so are dates. I chose to quote dates because I figured without them, it might be hard to tell if 6/15/12 was a date or a division of three numbers. When the optional argument is True (display formatted value), text, dates and numbers that are not "pure" are quoted. A pure number would be one that is composed of only digits, thousands separators (if present) and a single decimal point. If you disagree with any of these decisions, let me know and I'll modify the code accordingly.
Function ExpandFormula(Cell As Range, Optional UseCellFormatting As Boolean) As String
Dim IsText As Boolean, CellNavArrow As Range
Dim X As Long, Z As Long, OldShapeCount As Long, ArrowCount As Long
Dim CurrentSheetName As String, PreviousCell As String, StartSheet As String, CellVal As Variant
StartSheet = ActiveSheet.Name
Application.ScreenUpdating = False
Cell.Parent.Activate
CurrentSheetName = ActiveSheet.Name
If Cell.HasFormula Then
ExpandFormula = Replace(Cell.Formula, "$", "")
OldShapeCount = ActiveSheet.Shapes.Count
Cell.ShowPrecedents
ArrowCount = ActiveSheet.Shapes.Count - OldShapeCount
On Error Resume Next
Do
X = X + 1
IsText = False
If Cell.NavigateArrow(True, X).Parent.Name <> CurrentSheetName Then
Do
Z = Z + 1
Set CellNavArrow = Cell.NavigateArrow(True, X, Z)
If CellNavArrow.Count = 1 Then
CellVal = CellNavArrow.Value
If Not WorksheetFunction.IsNumber(CellVal) Then IsText = True
If IsText And Not CellVal Like "*[!0-9.]*" And Not CellVal Like "*.*.*" And _
Len(CellVal) > 0 And CellVal <> "." Then IsText = False
If UseCellFormatting Then
CellVal = WorksheetFunction.Text(CellNavArrow.Value, CellNavArrow.NumberFormat)
End If
If IsText Then CellVal = """" & CellVal & """"
ExpandFormula = Replace(ExpandFormula, "'" & CellNavArrow.Parent.Name & _
"'!" & CellNavArrow.Address(0, 0), CellVal)
ExpandFormula = Replace(ExpandFormula, CellNavArrow.Parent.Name & _
"!" & CellNavArrow.Address(0, 0), CellVal)
End If
Loop While Err.Number = 0
Else
Set CellNavArrow = Cell.NavigateArrow(True, X)
If CellNavArrow.Count = 1 Then
CellVal = CellNavArrow.Value
If UseCellFormatting Then
CellVal = WorksheetFunction.Text(CellNavArrow.Value, CellNavArrow.NumberFormat)
End If
If Not WorksheetFunction.IsNumber(CellVal) Then IsText = True
If IsText And Not CellVal Like "*[!0-9.]*" And Not CellVal Like "*.*.*" And _
Len(CellVal) > 0 And CellVal <> "." Then IsText = False
If IsText Then CellVal = """" & CellVal & """"
ExpandFormula = Replace(ExpandFormula, CellNavArrow.Address(0, 0), CellVal)
End If
End If
Worksheets(CurrentSheetName).Activate
Loop While X <= ArrowCount
Cell.ShowPrecedents Remove:=True
Else
If UseCellFormatting Then
ExpandFormula = WorksheetFunction.Text(Cell.Value, Cell.NumberFormat)
Else
ExpandFormula = Cell.Value
End If
End If
Worksheets(StartSheet).Activate
Application.ScreenUpdating = True
End Function