Thread: Calling macro?
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Calling macro?

In a macro it is possible to call another macro with the
statement 'Call', like

Call AnotherMacro

But, is it possible in the called macro ("AnotherMacro") to
get the name of the macro, which called "AnotherMacro"?


I don't believe so, well, at least not automatically. I believe the name of
subroutines, functions, etc., like variable names, get converted to memory
addresses and bundled into something called a "symbol table", so I do not
believe the names of these survive the compile process unless direct steps
are taken to do so. The steps depend on whether your AnotherMacro is really
a macro or not. Macro are meant to be called from the worksheet in some
manner (press ALT+F8, assign it to a button, etc.) and, as such, cannot have
any arguments. However, if your AnotherMacro will not be called this way,
that is, it will only be called from within other code, then you can give it
an argument and pass the calling routine's name through it. For example,

Sub MainMacro()
....
Call AnotherSubroutine("MainMacro")
....
End Sub

Sub AnotherSubroutine(WhoCalledMe As String)
....
MsgBox "I was called by " & WhoCalledMe
....
End Sub

Another possibility is to use a global variable to transmit the name... this
method can be used with real macros as well as subroutines. Put this line of
code at the top of the Module with your macros in it...

Dim WhoCalledMe As String

then this macro combination will do what you asked...

Sub MainMacro()
....
WhoCalledMe = "MainMacro"
Call AnotherMacro("MainMacro")
....
End Sub

Sub AnotherMacro()
....
MsgBox "I was called by " & WhoCalledMe
....
End Sub


Rick Rothstein (MVP - Excel)