What called a subroutine>

C

Chris Watts

Is there an easy method to identify from where a vba subroutine is called.
I want to us this to alter how the called subroutine executes. It could
have been called directly as a macro by the user or as a call from another
vba subroutine.
[Excel 2000 and 2007 under Windows XP]

TIA
Chris
 
K

Karl E. Peterson

Chris said:
Is there an easy method to identify from where a vba subroutine is called.
I want to us this to alter how the called subroutine executes. It could
have been called directly as a macro by the user or as a call from another
vba subroutine.

You can look at the Call Stack if you're debugging, but there's no way through code
to access that information. Only option is to create a trail you can follow.
 
C

Chris Watts

Karl E. Peterson said:
You can look at the Call Stack if you're debugging, but there's no way
through code to access that information. Only option is to create a trail
you can follow.
Thanks, Karl.
Plan B it is then!!
cheers
Chris
 
S

Steve Rindsberg

Is there an easy method to identify from where a vba subroutine is called.
I want to us this to alter how the called subroutine executes. It could
have been called directly as a macro by the user or as a call from another
vba subroutine.
[Excel 2000 and 2007 under Windows XP]

Sub MySub (Optional sCaller as String = "")
If Len(sCaller) = 0 Then
' Do the thing you want to do when called by the user
Else
' Do the other thing
MsgBox sCaller
End If
End Sub

Sub TestMySub()
MySub "TestMySub"
End Sub
 
C

Chris Watts

Ah very cunning!
Thanks, Steve
cheers
Chris

Steve Rindsberg said:
Is there an easy method to identify from where a vba subroutine is
called.
I want to us this to alter how the called subroutine executes. It could
have been called directly as a macro by the user or as a call from
another
vba subroutine.
[Excel 2000 and 2007 under Windows XP]

Sub MySub (Optional sCaller as String = "")
If Len(sCaller) = 0 Then
' Do the thing you want to do when called by the user
Else
' Do the other thing
MsgBox sCaller
End If
End Sub

Sub TestMySub()
MySub "TestMySub"
End Sub
 
K

Karl E. Peterson

Chris said:
Ah very cunning!

Yeah, that's the "Hänsel und Gretel" approach I referred to. <g> You could take it
to the next step, and create your own programatic call stack. (There are lots of
class-module based stack examples out there.) You just push the current routine
onto the stack on entry, and pop it back off on exit. Requires extreme discipline,
though.

Creating a Stack
http://msdn.microsoft.com/en-us/library/aa227509(VS.60).aspx

--
..NET: It's About Trust!
http://vfred.mvps.org


Steve Rindsberg said:
Is there an easy method to identify from where a vba subroutine is
called.
I want to us this to alter how the called subroutine executes. It could
have been called directly as a macro by the user or as a call from
another
vba subroutine.
[Excel 2000 and 2007 under Windows XP]

Sub MySub (Optional sCaller as String = "")
If Len(sCaller) = 0 Then
' Do the thing you want to do when called by the user
Else
' Do the other thing
MsgBox sCaller
End If
End Sub

Sub TestMySub()
MySub "TestMySub"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top