Solved

Getting the calling sub

Posted on 2010-09-15
9
312 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now