My "GetFormula" now a reserved word?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,535
Office Version
  1. 365
Platform
  1. Windows
I wrote a GetFormula UDF many years ago before Excel had the FormulaText function. Sometime in the past several months, it started getting a #VALUE error before it is even called. If I change the name, it works. Here's the code. Is "getformula" now a reserved word?

VBA Code:
Function GetFormula(pCell As Range, Optional pAddrSw As Boolean = True) As String

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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
GetFormula is not a reserved word, or built-in function, or built-in method for any object.

Your code above works fine for me as is.

it started getting a #VALUE error before it is even called
I can't make sense of that statement. You can't get a #VALUE error unless the function is called. When this happens with a UDF I write a Sub to call the UDF so I can control the execution, and step through it.
 
Upvote 0
I can only get the #VALUE, by providing the parameter "pAddrSw" as a Cell reference with a value in it that can not be interpreted as being Boolean eg a text value other than TRUE & FALSE

20220410 VBA UDF Failing JenniferMurphy.xlsm
ABCDEF
2Value1Value2FormulaParameter pAddrSwGetFormula TextFormulaTxt
310100110C3: =A3+B3=GetFormula(C3)
410100110TRUEC4: =A4+B4=GetFormula(C4,D4)
510100110FALSE=A5+B5=GetFormula(C5,D5)
6101001101C6: =A6+B6=GetFormula(C6,D6)
7101001100=A7+B7=GetFormula(C7,D7)
810100110X#VALUE!=GetFormula(C8,D8)
Sheet1
Cell Formulas
RangeFormula
E3E3=GetFormula(C3)
F3:F8F3=FORMULATEXT(E3)
E4:E8E4=GetFormula(C4,D4)
C3:C8C3=A3+B3
 
Upvote 0
GetFormula is not a reserved word, or built-in function, or built-in method for any object.

Your code above works fine for me as is.
Curious

I can't make sense of that statement. You can't get a #VALUE error unless the function is called. When this happens with a UDF I write a Sub to call the UDF so I can control the execution, and step through it.
What I mean is that I set a breakpoint on the function definition (line 1). When I execute the cell that calls it, the breakpoint never happens, so I assume that the function is never called.

I made a copy of the function in the same add-in module and changed the name. That version works. Here's that code and a mini-sheet:

VBA Code:
Function GetFormulaX(pCell As Range, Optional pAddrSw As Boolean = True) As String

GetFormulaX = pCell(1).FormulaArray      'Get the formula in the cell
If pCell(1).HasArray Then               'If it's an array formula,
  GetFormulaX = "{" & GetFormulaX & "}"     '.add the {}s
End If

If pAddrSw Then   'If switch is on, add the cell address and leading quote, if any
  GetFormulaX = pCell(1).Address(0, 0) & ": " & pCell(1).PrefixCharacter & GetFormulaX
End If

End Function

Book1
BCD
39#VALUE!B3: =4+5
Sheet1
Cell Formulas
RangeFormula
B3B3=4+5
C3C3=getformula(B3)
D3D3=getformulaX(B3)
 
Upvote 0
I can only get the #VALUE, by providing the parameter "pAddrSw" as a Cell reference with a value in it that cannot be interpreted as being Boolean eg a text value other than TRUE & FALSE
As you can see from the mini-sheet above, I am not passing that parameter.

What's odd is that it was working until a few months ago. ??
 
Upvote 0
Does it only happen in the one workbook ?
Do have a named range using that name ?
Do have any addins loaded, can you try disabling them ?
 
Upvote 0
Solution
Here is what I get in the exact same setup.

$scratch.xlsm
BCD
39B3: =4+5B3: =4+5
Sheet5
Cell Formulas
RangeFormula
B3B3=4+5
C3C3=GetFormula(B3)
D3D3=getformulaX(B3)
 
Upvote 0
Does it only happen in the one workbook ?
Do have a named range using that name ?
Do have any addins loaded, can you try disabling them ?
I tried it in several workbooks -- same result.

The mini-sheet I posted above was in a brand new workbook with nothing but that example, so no named ranges.

The only add-ins are Name Manager, XL2BB, and my add-in.
1649623872034.png


I remember trying Name Manager a long time ago, but I had some problems. I thought I unloaded it. I unchecked that box and the problem was solved. I tried to open Name Manager.xlam, but it's locked. I then tried reinstalling it. My GetFormula still works. But since I am not using it, I unchecked it.

It's still puzzling why is started failing just recently. I don't recall checking that box. Perhaps by accident.

In any case, problem solved. thanks.
 
Upvote 0

Forum statistics

Threads
1,215,419
Messages
6,124,796
Members
449,189
Latest member
kristinh

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