+ Reply to Thread
Results 1 to 9 of 9

Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

  1. #1
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    Hi Everyone,

    I have a question on moving from Sub-procedures back to the main macro.

    I have a database/dashboard with a Userform to manage data entry. Many of the userform fields have exclusion criteria. I.e. A message box would appear if you entered an insurance company but also checked "no insurance" to tell the user they have to choose one or the other and then exiting the sub (but not the userform) so they can make the correction.

    Because this userform has 7 periods of followup after a clients intake, I had to breakdown parts of the code into sub-procedures because I received the "Procedure Too Large" error.

    The problem I have is that message box exclusion criteria within the sub-procedures exits the entire userform (not allowing the user to make the correction). The message box will show correctly, but upon exiting that message box, the whole userform closes too.

    Is there a way to use the "Exit Sub" after a message box in a sub-procedure and return to the larger macro without the userform closing?

    Please Login or Register  to view this content.
    Thanks in advance!

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    .
    Try calling the UserForm in question from the other sub ...

  3. #3
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    Hi Logit,

    The genius simplicity of your solution made me kick myself (one of those "of course!!" moments) but it appears to have had the following effects:

    If the call is after the "Exit Sub" following the message box, the userform closes as it did before (with no discernible difference).

    If I put the call before "Exit Sub" following the message box, I get an error that the userform cannot be called/duplicated because its already open.

    Thoughts?

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    .
    Are you "HIDE" the form or are you "Unload Me" the form ?

    Unload Me should close it and remove it from memory.
    Last edited by Logit; 09-14-2018 at 10:04 AM.

  5. #5
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    Hi Logit,

    If there are no validation issues, the form is unloaded. With any of the validation prompts, I always have it "Exit Sub" so the user can fix the issue in the userform.

    If a validation issue occurs within one of the called procedures, this appears to simply end everything instead of allowing the user to correct the issue in the userform and hit the "Save" button again.

  6. #6
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    Hi Everyone,

    I have found a work-around that suits me for now. I simply moved all validation from the sub-procedures into the main Sub, so that no validation occurs in a called procedure. This appears to have worked. I'm marking this complete for moment, though I wouldn't mind knowing if there was a way to achieve what I had originally asked.

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    None of the code you posted would close the userform so it's hard to say. Did you use End on its own somewhere?
    Rory

  8. #8
    Forum Contributor
    Join Date
    05-23-2013
    Location
    Ohio
    MS-Off Ver
    Excel 2013
    Posts
    259

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    Hi Rorya,

    End was only used at the end of the main sub.

    What I imagine was happening is that using "Exit Sub" in the called procedure only exited that single called procedure. I think the main sub simply continued chugging along once that called procedure was stopped. Since the validation prompts in question were contained within each sub procedure, as long as there weren't other validation issues in the main sub, it simply completed the operation. But calling the main sub from one of the called procedures didn't work because it was still in the process of running.

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Returning to Main Macro from a Called Procedure after a MsgBox/Exit Sub

    In that case I would have recommended changing the subs into functions that return True if all OK and False otherwise. You could then test each result before proceeding with the rest of the code.

+ 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. [SOLVED] Exit Sub Procedure when called on Sub Procedure Exits
    By onmyway in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2015, 08:17 AM
  2. Procedure skips portion of code when called from another procedure
    By abkar in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-26-2014, 05:14 PM
  3. [SOLVED] How to Exit Sub if called function has error?
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-16-2014, 03:33 PM
  4. to exit main procedure from procedure called by yhe main
    By kmlprtsngh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-27-2010, 02:46 PM
  5. Determine Which Check Box Called Procedure
    By jasoncw in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-21-2009, 05:06 PM
  6. [SOLVED] procedure continuously being called
    By nathan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-19-2006, 04:45 PM
  7. [SOLVED] private sub- which procedure called it
    By Sunil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2005, 08:05 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