+ Reply to Thread
Results 1 to 4 of 4

How can SUB know that it was called?

Hybrid View

sauerj How can SUB know that it was... 09-30-2010, 08:15 PM
MarvinP Re: How can SUB know that it... 09-30-2010, 08:47 PM
jaslake Re: How can SUB know that it... 09-30-2010, 08:50 PM
sauerj Re: How can SUB know that it... 09-30-2010, 09:15 PM
  1. #1
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2019
    Posts
    163

    Lightbulb How can SUB know that it was called?

    What is the easiest way for a SUB to know that it was called by other macro?
    Right now, I use the following technique (and send a TRUE to 'Called' if so, else FALSE), but there has to be a more direct way. ... Any ideas?

    Sub CallingSub()
       blah ... blah ... blah
      Call CalledSub(Called:=True)
       blah ... blah ... blah
    End Sub
    
    Sub CalledSub(Optional Called As Boolean = False)
     If Called = True Then GoTo Label_100
       < do code that only matters if this sub was NOT Called >
       < GoTo Label_200 >
    Label_100
       < do code that only matters if this sub was Called >
    Label_200
    End Sub
    Last edited by sauerj; 10-01-2010 at 10:53 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,379

    Re: How can SUB know that it was called?

    How about using a global variable that keeps track?

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: How can SUB know that it was called?

    Assuming Label_100 is a procedure, my personal preference is
    Call Label_100
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2019
    Posts
    163

    Re: How can SUB know that it was called?

    I edited the code shown on initial POST (above) so to better show how the Caller & Called SUBS interact.

    That said, I like MarvinP's idea. That would be a fairly simple approach. Right before calling a SUB, add '1' to a Public Global variable. Then, when returning, subtract '1' from this variable. Then, if any SUB is running and this variable is > 0, then it has to have been called. Seems pretty solid to me. Thanks MarvinP.

    I thought there might be some ERROR internal checker that tracks if a SUB was called (or some sort of internal VB thing). ... I'll keep this POST unsolved for another day or so in case someone else throws out another idea.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1