Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 143
Default 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.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default 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.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default 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.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 142
Default 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.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Print list of ws names Joanne New Users to Excel 3 October 8th 07 03:16 PM
How to print out matching names? Tom Excel Programming 3 September 14th 07 10:47 PM
Print to PDF with SHEET names [email protected] Excel Discussion (Misc queries) 5 April 10th 07 01:57 PM
Print range names and how know if have too many? John Excel Worksheet Functions 3 January 16th 06 06:44 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"