Solved

Getting the calling sub

Posted on 2010-09-15
9
321 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
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 500 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 33

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

821 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