• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1385
  • Last Modified:

Caller Function Name - Excel VBA

Hello All,

Is there a way to detect the caller function’s name and show it – for example if myFunc2 calls another function [myFunc3], I can also see a message box like :
Executing …myFunc3, this function was originally called by myFunc2


thank you all
Respect
R
callerName.xlsm
0
Rayne
Asked:
Rayne
  • 2
  • 2
2 Solutions
 
Saqib Husain, SyedEngineerCommented:
0
 
RyanProject Engineer, ElectricalCommented:
I briefly looked at the link ssaqibh posted, and it does look like it may do what you're asking, although it was kind of hard to follow.

A simple solution, which is exactly how function calls work is to store them on a stack (LIFO -Last in First Out).  Opposite of a queue.

If you have a global stack object, and every function PUSHES its name onto the stack when it starts, and POPS itself off the stack when it exits, then at any point you'll have the "Stack Trace" of your function calls.

Maybe there's a way to access the real stack trace directly, but this general idea is what is happening.
0
 
RayneAuthor Commented:
Thanks everyone, how do I get Ssaqibh's code to display a message?

Option Explicit
Public stLastProc As String
Public boExecutionDebug As Boolean
Public lnStartLine As Long

Sub InitiateCodeExecutionAnalysis()
  stLastProc = Empty
  boExecutionDebug = True
End Sub
Sub MySubRoutine()
  Application.VBE.ActiveCodePane.GetSelection lnStartLine, 1, 1, 1
  If boExecutionDebug = True Then ExecutionDebug lnStartLine, Application.VBE.ActiveCodePane.CodeModule.Name
End Sub
Sub MySubRoutine2()
  Application.VBE.ActiveCodePane.GetSelection lnStartLine, 1, 1, 1
  If boExecutionDebug = True Then ExecutionDebug lnStartLine, Application.VBE.ActiveCodePane.CodeModule.Name
End Sub
Sub ExecutionDebug(lnLine As Long, stModuleName As String)
  Dim stActiveSubName As String
  stActiveSubName = Application.VBE.ActiveVBProject.VBComponents(stModuleName).CodeModule.ProcOfLine(lnLine, vbext_pk_Proc)
  If stLastProc <> Empty Then
    MsgBox "Procedure: " & stLastProc & ""
  End If
  stLastProc = stActiveSubName
End Sub



Sub Button1_Click()
Call myFunc
End Sub


Sub myFunc()

Call myfunc2
'show myFunc (caller function's name...)

End Sub

Sub myfunc2()

MsgBox "Hello ALL, please take a seat"
Call myfunc3
'show myFunc2 (caller function's name...)

End Sub

Sub myfunc3()

MsgBox "Hello All, please take a cup of coffee and relax"


End Sub
0
 
RayneAuthor Commented:
The code seems a hard thing to follow
0
 
RyanProject Engineer, ElectricalCommented:
Here's code to implement a stack.  I tried to understand how that other code works, sorry.

http://www.tek-tips.com/viewthread.cfm?qid=1468970
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now