Solved

Getting the calling sub

Posted on 2010-09-15
9
320 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Spacing Anomaly 4 24
Excel IF formula 3 21
VLOOKUP 6 18
What is the best way to re-number row value from 1 to visible rows only in excel 6 19
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

803 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