?
Solved

Getting the calling sub

Posted on 2010-09-15
9
Medium Priority
?
334 Views
Last Modified: 2012-08-13
Is it possible, in a sub or function, to get the name of the routine or function which is calling it?

I know it can return how it was called from excel by the application.caller method.

Saqib
0
Comment
Question by:Saqib Husain, Syed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 33689174
Saqib,

I posted the link for it might help you. It discusses re: application.caller method.

http://www.cpearson.com/excel/sheetref.htm

Sincerely,

Ed
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 2000 total points
ID: 33689196
Saqib,

I've tested this technique and it works - I'm quite interested in it as it uses an approach I haven't see before

http://www.ozgrid.com/forum/showthread.php?t=55188&page=1

Another way would be to store the current module name in a Public variable before calling the subroutine.

Cheers

Dave


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
    Debug.Print "Procedure: " & stLastProc & ""
  End If
  stLastProc = stActiveSubName
End Sub

Open in new window

0
 
LVL 34

Expert Comment

by:Norie
ID: 33693793
Saqib

Why do you want/need to know the name of the calling sub?

I'm pretty sure tou can't use Application.Caller, which is pretty limited anyway.

I would think the code Dave posted might be of some use but I've not seen the method either and it looks like the
code is manipulating the VBE in some way.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 33883465
Dave,

This solution is a bit too complex for me and I need more time to try it out. Sorry for holding you up

Saqib
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34065461
Object as the solution I provided works, and the simple workaround " store the current module name in a Public variable" in the same comment (a33689196.html) would have sufficed if the detailed solution was deemed too complex

Cheers

Dave
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34079912
I give up. Did not get to try it. Will come back whenever I try it. Thanks for the patience and sorry for the holdup.

Saqib
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 34079919
closing
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question