JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,531
- Office Version
- 365
- Platform
- Windows
Some time ago I wrote a little UDF to display the formula in a cell. It works great, but I discovered that it does not detect array formulas. I added an optional parameter so I could get it to add the "{}".
This was a poor solution. It would be great if it could do it automatically. So I scanned the Cell properties and discovered Cell.HasArray and modified the code thus:
This seems to handle array formulas, but then I noticed that it doesn't handle strings that look like formulas ('=x-1). After a little more research, I added code to check for VarType 8:
This seems to work. Can anyone find any holes in the logic?
This is for Office 2007. I understand that Office 2013 has a FormulaText function that may make this unnecessary.
Code:
Function GetFormula(Cell As Range, Optional arraysw = "") As String
GetFormula = Cell.Formula
If UCase(arraysw) = "ARRAY" Then GetFormula = "{" & GetFormula & "}"
End Function
This was a poor solution. It would be great if it could do it automatically. So I scanned the Cell properties and discovered Cell.HasArray and modified the code thus:
Code:
Function GetFormula(Cell As Range) As String
GetFormula = Cell.Formula
If Cell.HasArray Then GetFormula = "{" & GetFormula & "}"
End Function
This seems to handle array formulas, but then I noticed that it doesn't handle strings that look like formulas ('=x-1). After a little more research, I added code to check for VarType 8:
Code:
Function GetFormula(Cell As Range) As String
GetFormula = Cell.Formula
If Cell.HasArray Then
GetFormula = "{" & GetFormula & "}"
ElseIf VarType(Cell) = 8 Then
GetFormula = "'" & Cell.Text
End If
End Function
This seems to work. Can anyone find any holes in the logic?
This is for Office 2007. I understand that Office 2013 has a FormulaText function that may make this unnecessary.