+ Reply to Thread
Results 1 to 3 of 3

How do I exit all nested subs without losing static variables?

Hybrid View

jason.b75 How do I exit all nested subs... 04-18-2019, 03:56 PM
KOKOSEK Re: How do I exit all nested... 04-18-2019, 04:01 PM
jason.b75 Re: How do I exit all nested... 04-18-2019, 04:09 PM
  1. #1
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    How do I exit all nested subs without losing static variables?

    Hi folks!

    Title says it all, but to clarify, I have something like (simplified).
    Sub Marco1()
         Call Macro2
         Call Macro3
    End Sub
    
    Sub Macro2()
        If this Then
            do that
        Else
            Exit Sub
        End If
    End Sub
    What I need is for the Exit Sub line to terminate both Macro2 and Macro1 without executing Macro3.
    I have static variables that need to be preserved so using End is not an option.
    I know that I can pass a Boolean back to the previous procedure, then use that to exit, but would like to know it there is a simple alternative method that mr google is hiding from me

    Thanks in advance.

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: How do I exit all nested subs without losing static variables?

    Global var?

    Sub Macro2()
        If this Then
            do that
        Else
            MyGlobalVar="do not run macro3"
            Exit Sub
        End If
    End Sub
    
    Sub Macro3
           if MyGlobalVar="do not run macro3" then Exit Sub                                        '1st line in macro3
    End Sub
    I am not sure as I am VBA noob.

    EDIT: I am BLINDnoob, as you mention about it in last line.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: How do I exit all nested subs without losing static variables?

    That's kind of what I was thinking where I mentioned passing a boolean back to the previous procedure.

    I'm curious as to whether there is a single 'Exit' command that will do what I need without the extra lines of code.

    edit:- As a workaround I've used a module level boolean set to True in place of Exit Sub in the nested procedures, then tested that after it drops back to the previous level to determine the exit status.

    Would still like to know if there is a direct command that I haven't found which will achieve the desired result. Will hold back for a couple of days before flagging thread as solved.
    Last edited by jason.b75; 04-18-2019 at 04:42 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Using variables from within Subs
    By mqubel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2018, 11:31 AM
  2. [SOLVED] user formm cancel button to exit multiple running subs
    By vijaysharmapc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2013, 02:55 PM
  3. Userform cancel button - how to exit multiple subs
    By sotiris5000 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-16-2010, 09:56 AM
  4. Exit all subs
    By boc_est1986 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-18-2009, 07:53 AM
  5. How to exit a series of subs if cancel is pressed
    By SOS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2008, 04:36 AM
  6. exit all subs? (from embedded sub)
    By KR in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-06-2006, 04:37 PM
  7. exit all subs? (from embedded sub)
    By KR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-06-2006, 10:40 AM

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