?
Solved

Know which sub is calling the target function?

Posted on 2012-09-11
13
Medium Priority
?
428 Views
Last Modified: 2012-09-12
Hello,

I have zillions of functions / subs in multiple vba module. I wrote a function (functionA) moths back and I know which module the function lives in. the issue is I am not sure which sub or function is calling my functionA – I really want to know which specific function is calling functionA. Is there a way to know it? Whichever way it permits me? Because I need to go to that caller function and make changes….any ideas?
I am confused.

thank you
0
Comment
Question by:Rayne
  • 5
  • 4
  • 4
13 Comments
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38389227
Is this a one-time situation? In other words are you trying to debug a problem?

If not then one thing you could do would be to add a parameter to functionA and pass the name of the calling function from each of the procedures that call functionA.
0
 

Author Comment

by:Rayne
ID: 38389231
Something like ....

Option Explicit

Function myCallerFunction()
'Debug.Print Application.Caller.Name
Call callingThis

End Function




Function callingThis()

Dim dm As Variant

dm = Application.Caller.Parent.Parent.Name
' dm basically stores the string [myCallerFunction]

MsgBox "the caller is " & dm

End Function
0
 

Author Comment

by:Rayne
ID: 38389232
still cant get it to work
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 50

Expert Comment

by:Martin Liss
ID: 38389233
Before someone beats me to it :) if you are debugging you could add a breakpoint at the first line in functionA and then use Debug to look at the call stack. For more on debugging take a look at my article, most of which applies to VBA.
0
 

Author Comment

by:Rayne
ID: 38389236
The issue is I am not sure which function or sub is calling FunctionA, so how it can be done ...
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38389238
Again let me ask. Are you having a problem that you're trying to debug?
0
 
LVL 50

Assisted Solution

by:Martin Liss
Martin Liss earned 700 total points
ID: 38389257
I'm going to assume you are trying to debug a problem and you don't know how to use Debug, so here's what you do.

Go to functionA and click in the left had margin next to the first line (the Private Function line) and click the margin. That will create a little red circle which indicates that a breakpoint has been set.

Run the program and when the code gets to the breakpoint go to View|Call stack and there you will see the procedures that led to you getting to the breakpoint
0
 
LVL 46

Expert Comment

by:aikimark
ID: 38389439
You could get by with this cheap solution.  If I start the code with either routine b or c, there will be two items in the collection with the last item being "a".  The item immediately prior will be the calling routine.
Option Explicit
Dim LastRtn As New Collection

Sub a(): LastRtn.Add "a"
Stop
End Sub

Sub b(): LastRtn.Add "b"
    a
End Sub

Sub c(): LastRtn.Add "c"
    a
End Sub

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 38389454
Just to be sure, I tried this with a function in the mix.
Option Explicit
Dim LastRtn As New Collection

Sub a(): LastRtn.Add "a"
    Dim vItem As Variant
    Debug.Print LastRtn.Count
    For Each vItem In LastRtn
        Debug.Print vItem
    Next
End Sub

Sub b(): LastRtn.Add "b"
    c
End Sub

Sub c(): LastRtn.Add "c"
    Debug.Print d
End Sub

Function d() As String: LastRtn.Add "d"
    a
    d = "Done"
End Function

Open in new window


When I start the execution with the b routine, the Immediate window contains these results
 4 
b
c
d
a
Done

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 38389463
Since you are working with multiple modules, you'd probably prefix the name of the routine with the name of the module/form.
Examples:
LastRtn.Add "module1.d"
LastRtn.Add "module1 d"
LastRtn.Add "module1-d"
LastRtn.Add "module1^d"

Open in new window

You would want to make the parsing easier by using a delimiter that couldn't be used as part of the module name.
0
 
LVL 46

Accepted Solution

by:
aikimark earned 1300 total points
ID: 38389488
Having gone this far, I now realize that I've probably reinvented the push and pop functionality in the linked text.  Here's the example with a pop.
Option Explicit
Dim LastRtn As New Collection

Sub a(): LastRtn.Add "a"
    Dim vItem As Variant
    Debug.Print LastRtn.Count
    For Each vItem In LastRtn
        Debug.Print vItem
    Next
Call RemoveLast("a"): End Sub

Sub b(): LastRtn.Add "b"
    c
Call RemoveLast("b"): End Sub

Sub c(): LastRtn.Add "c"
    Debug.Print d
Call RemoveLast("c"): End Sub

Function d() As String: LastRtn.Add "d"
    a
    d = "Done"
Call RemoveLast("d"): End Function

Sub RemoveLast(parmRtnName)
    Dim lngLoop As Long
    For lngLoop = LastRtn.Count To 1 Step -1
        If LastRtn(lngLoop) = parmRtnName Then
            LastRtn.Remove lngLoop
            Exit For
        End If
    Next
End Sub

Open in new window


Note: any Exit Sub or Exit Function statements would also need to tweaked

Example:
Call RemoveLast("d"): Exit Function

Open in new window

0
 

Author Comment

by:Rayne
ID: 38389868
Hello Martin & Aikimark,

First of all, sorry for the delayed reply, I was away from my machine. I greatly appreciate the time and effort all of you gave to help me out. Thank you for your diligence and effort to make this issue as clear as possible :)

Respectfully,
Rayne
0
 
LVL 50

Expert Comment

by:Martin Liss
ID: 38391402
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2012
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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…

809 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