ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can a Sub/Function print the names of its callers? (https://www.excelbanter.com/excel-programming/443338-can-sub-function-print-names-its-callers.html)

[email protected]

Can a Sub/Function print the names of its callers?
 
If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. Line numbers would be great too if that is
possible.

Example:

sub Main()
call Sub
end sub

sub Sub1()
call sub2(3)
end sub

Sub Sub2(n as long)
if n 2 then
'Code to print
' Sub 2 called with value n
' from Sub1 at line 2
' from Main at line 2
end
end if
end sub


Ron Rosenfeld[_2_]

Can a Sub/Function print the names of its callers?
 
On Tue, 13 Jul 2010 13:43:35 -0400, wrote:

If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. Line numbers would be great too if that is
possible.

Example:

sub Main()
call Sub
end sub

sub Sub1()
call sub2(3)
end sub

Sub Sub2(n as long)
if n 2 then
'Code to print
' Sub 2 called with value n
' from Sub1 at line 2
' from Main at line 2
end
end if
end sub


Check out the UserName property of the Application object.

[email protected]

Can a Sub/Function print the names of its callers?
 
On Tue, 13 Jul 2010 14:18:05 -0400, Ron Rosenfeld wrote:

On Tue, 13 Jul 2010 13:43:35 -0400, wrote:

If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. Line numbers would be great too if that is
possible.

Example:

sub Main()
call Sub
end sub

sub Sub1()
call sub2(3)
end sub

Sub Sub2(n as long)
if n 2 then
'Code to print
' Sub 2 called with value n
' from Sub1 at line 2
' from Main at line 2
end
end if
end sub


Check out the UserName property of the Application object.


Apparently I am not understanding your reply. Application.UserName returns
the name of my computer, not the names in the sub/functuion call chain.

James Ravenswood

Can a Sub/Function print the names of its callers?
 
On Jul 13, 1:43*pm, wrote:
If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. *Line numbers would be great too if that is
possible.

Example:

sub Main()
* * * * call Sub
end sub

sub Sub1()
* * * * call sub2(3)
end sub

Sub Sub2(n as long)
* * if *n 2 then
* * * * 'Code to print
* * * * ' * *Sub 2 called with value n
* * * * ' * from *Sub1 at line 2
* * * * ' * from Main at line 2
* * * * end
* * end if
end sub


You can always "remember" the caller if its name is not exposed:

Dim caller As String

Sub main()
caller = "main"
Call sub1
End Sub

Sub sub1()
MsgBox caller
'do other stuff
End Sub

In fact, you could even implement a Stack and have each sub push its
name onto the stact prior to calling another sub and poping the name
after the other sub's return.

Ron Rosenfeld[_2_]

Can a Sub/Function print the names of its callers?
 
On Tue, 13 Jul 2010 15:13:57 -0400, wrote:

Apparently I am not understanding your reply. Application.UserName returns
the name of my computer, not the names in the sub/functuion call chain.


Sorry. I did not understand your question. Probably James' reply
will help you.

[email protected]

Can a Sub/Function print the names of its callers?
 
On Tue, 13 Jul 2010 12:42:35 -0700 (PDT), James Ravenswood
wrote:

On Jul 13, 1:43*pm, wrote:
If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. *Line numbers would be great too if that is
possible.

Example:

sub Main()
* * * * call Sub
end sub

sub Sub1()
* * * * call sub2(3)
end sub

Sub Sub2(n as long)
* * if *n 2 then
* * * * 'Code to print
* * * * ' * *Sub 2 called with value n
* * * * ' * from *Sub1 at line 2
* * * * ' * from Main at line 2
* * * * end
* * end if
end sub


You can always "remember" the caller if its name is not exposed:

Dim caller As String

Sub main()
caller = "main"
Call sub1
End Sub

Sub sub1()
MsgBox caller
'do other stuff
End Sub

In fact, you could even implement a Stack and have each sub push its
name onto the stact prior to calling another sub and poping the name
after the other sub's return.


Thanks for your reply. I was hoping to use the capability to help generate
a message when a routine somewhere down a call chain detected an unexpected
error. It would be used with already written routines.

I know that VBA must already maintain a call stack. I was hoping that, in
it might contain caller names & perhaps line numbers and that I could
access info in the stack. It sounds like there is no such capability.

You are, of course, correct that I could maintain my own stack but that
would require code before and after each call which would be difficult for
subs and much more clumsy when using functions.
i = f(3 + g(4)) + h(3.14159, SomethingElse)

I was hoping that being an intrepreted rather than compiled language (at
least to a degree) VBA might include such a capability.
Thanks anyway.

[email protected]

Can a Sub/Function print the names of its callers?
 
On Tue, 13 Jul 2010 12:42:35 -0700 (PDT), James Ravenswood
wrote:

On Jul 13, 1:43*pm, wrote:
If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. *Line numbers would be great too if that is
possible.

Example:

sub Main()
* * * * call Sub
end sub

sub Sub1()
* * * * call sub2(3)
end sub

Sub Sub2(n as long)
* * if *n 2 then
* * * * 'Code to print
* * * * ' * *Sub 2 called with value n
* * * * ' * from *Sub1 at line 2
* * * * ' * from Main at line 2
* * * * end
* * end if
end sub


You can always "remember" the caller if its name is not exposed:

Dim caller As String

Sub main()
caller = "main"
Call sub1
End Sub

Sub sub1()
MsgBox caller
'do other stuff
End Sub

In fact, you could even implement a Stack and have each sub push its
name onto the stact prior to calling another sub and poping the name
after the other sub's return.


Thanks for your reply. I was hoping to use the capability to help generate
a message when a routine somewhere down a call chain detected an unexpected
error. It would be used with already written routines.

I know that VBA must already maintain a call stack. I was hoping that, in
it might contain caller names & perhaps line numbers and that I could
access info in the stack. It sounds like there is no such capability.

You are, of course, correct that I could maintain my own stack but that
would require code before and after each call which would be difficult for
subs and much more clumsy when using functions.
i = f(3 + g(4)) + h(3.14159, SomethingElse)

I was hoping that being an intrepreted rather than compiled language (at
least to a degree) VBA might include such a capability.
Thanks anyway.


All times are GMT +1. The time now is 10:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com