Few UDF (VBA) questions - are there tricks for this?

TomCon

Active Member
Joined
Mar 31, 2011
Messages
385
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Or, at least i'd think of them as tricks, since it is not obvious to me!

o Is there any way to find the address of the cell that is currently calling the UDF? Say, you set a breakpoint, and want to know which cell it is now calculating when it hits the breakpoint.
o Is there a way to set a conditional breakpoint on a particular cell as calling the UDF? For instance, only break when this function is called from cell A5, etc? (In the VBA editor).
o Is there a way to get the text of the argument that is in the UDF call from the sheet? For instance, if my UDF is declared as Function MyFun(arg1). Say, on the sheet it is called with =MyFun(Max(A5:A10)). In the function execution code, i will get arg1 as the value of Max(A5:A10). Within UDF(VBA) code, can i get at the text for the argument, which in this example is "Max(a5:a10)"?

Thanks,
tom
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
It's hard to tell from that description what you are trying to accomplish exactly. But I think this answers your questions:


Code:
Function MyFunction(f As String) As String
Dim a As Range
Set a = Parent.Caller
If a.Address = "$A$5" Then Stop
MyFunction = f
End Function
 
Upvote 0
Thanks much! That answers the first two bullets. Great, glad to see it exists; i feared it did not. These are fairly general questions, but i have run into this need several times, so just thought i'd ask.

On the third bullet, does anybody know if there is a way to do that?

Thanks!
Tom
 
Upvote 0
I gave you that too, just pass the inside as a string.


=myfunction("MAX(A1:A5)")
 
Upvote 0
Another way:
Code:
Function myFunc(arg) As String
    myFunc = Application.ThisCell.Formula
    myFunc = Mid(myFunc, InStr(myFunc, "(") + 1)
    myFunc = Left(myFunc, InStrRev(myFunc, ")") - 1)
End Function

=MyFunc(Max(A5:A10)) returns the string "Max(A5:A10)"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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