Solved

Getting the calling sub

Posted on 2010-09-15
9
324 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 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 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
Independent Software Vendors: 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

Industry Leaders: 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!

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…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

739 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