Having trouble calling FormulaText from VBA

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. 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:

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
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
VBA Code:
Function GetFormula(pCell As Range, Optional pAddrSw As Boolean = True) As String
    If pAddrSw Then   'If switch is on, add the cell address and leading quote, if any
        GetFormula = pCell.Address(0, 0) & ": " & pCell.Formula
    Else
        GetFormula = pCell.Formula
    End If
End Function
 
Upvote 0
What's wrong with the first statement that gets the error?
Not all worksheet functions are available to the vba WorkSheetFunction object.
See List of worksheet functions available to Visual Basic

You could also note that that function does not appear in the intellisense list

1650954302139.png
 
Upvote 0
Another way of writing the function

VBA Code:
Function GetFormula(pCell As Range, Optional pAddrSw As Boolean = True) As String
  GetFormula = IIf(pAddrSw, pCell.Address(0, 0) & ": ", "") & pCell.Formula
End Function
 
Upvote 0
Solution
Another way of writing the function

VBA Code:
Function GetFormula(pCell As Range, Optional pAddrSw As Boolean = True) As String
  GetFormula = IIf(pAddrSw, pCell.Address(0, 0) & ": ", "") & pCell.Formula
End Function
Perfect. Clean and compact. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,301
Members
449,078
Latest member
nonnakkong

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top