JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,532
- Office Version
- 365
- Platform
- Windows
A long time ago, I wrote a GetFormula UDF that does what the FormulaText function now does. But my code has the additional advantage that it will optionally display the address of the cell being referenced. So instead of just "=C7*D7" I get "E7: =C7*D7".
I am trying to convert it to use the FormulaText function, but it keeps getting a #VALUE error. Here's the code:
The previous code that worked is shown commented out. What's wrong with the first statement that gets the error?
Thanks
I am trying to convert it to use the FormulaText function, but it keeps getting a #VALUE error. Here's the code:
VBA Code:
Function GetFormula(pCell As Range, Optional pAddrSw As Boolean = True) As String
GetFormula = Application.WorksheetFunction.formulatext(pCell) 'This gets a #VALUE error
'GetFormula = pCell(1).FormulaArray 'Get the formula in the cell
'If pCell(1).HasArray Then 'If it's an array formula,
' GetFormula = "{" & GetFormula & "}" '.add the {}s
'End If
If pAddrSw Then 'If switch is on, add the cell address and leading quote, if any
GetFormula = pCell(1).Address(0, 0) & ": " & pCell(1).PrefixCharacter & GetFormula
End If
End Function
The previous code that worked is shown commented out. What's wrong with the first statement that gets the error?
Thanks