+ Reply to Thread
Results 1 to 15 of 15

How to Exit Sub if called function has error?

  1. #1
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    How to Exit Sub if called function has error?

    Hi, below is one example of several macros I'm using in a workbook. Instead of several macros all having duplicate lines of code, I've decided to use called functions; it makes my life much easier for updating things...

    However, one thing I've encountered, is if there is an error in one of the called functions, the macro will continue to run to the macro completion. How can I make the code below stop itself dead in it's tracks if one of the called functions results in an error?

    Also, each called function has the same errorhandling as this macro, and it does work. Thus I will know what line in which called function the error came from. I would just like the TEST_MACRO to stop running once an error is encountered.

    If at all possible, I would like to accomplish this while retaining the error reporting for TEST_MACRO as well.

    Thanks in advance for any help!

    Please Login or Register  to view this content.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    I usually define a Public Global boolean variable, say, bError. Set it to False before calling your subroutines/functions. In the error handler of each subroutine/function, set it to True (if the error is terminal). After each subroutine/function call, test the variable. Something like: If bError Then Exit Sub ... or If bError Then GoTo lblMainErrorHandler


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    Hi TMS, thanks for your reply! I comprehend what you're saying to do, but I'm not entirely positive I know how to execute it. I know enough about VBA to be dangerous, but far from what I'd considered knowledgeable...

    Something like this perhaps???

    Please Login or Register  to view this content.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    Close

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    Aah, okay!

    So does the "Public bError As Boolean" go above each macro that has called functions, or simply in the module somewhere?

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    "Public bError As Boolean" appears once, and once only, before any of your procedures. or it could go in a separate module.

    Because it is defined outside all the functions and subroutines, its scope is Global and can be seen and modified by any of the routines.


    Regards, TMS

  7. #7
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    Got it; thanks for the clarification. One more question, regarding the "bError = True" statement in the called functions, so I put that once at the top of each function?

    Thanks!

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    regarding the "bError = True" statement in the called functions, so I put that once at the top of each function?
    No, that goes in the error handler in the subroutine. So, if you get an error in the subroutine, it does whatever it needs to do to tidy up, display an error message, whatever AND sets the bError flag to True so that it can be picked up in the main routine.

    Regards, TMS

  9. #9
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    So something like this?

    Please Login or Register  to view this content.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    Yep, that looks about right.

  11. #11
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    Great! I'll go doctor up a few macros and functions, run some tests, then follow up with my results.

    With any luck, my next post will be reveling in your awesomeness!

    Thanks for the help so far!

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    Actually, if you have a common error handler, you could set the flag in that. No need to do it in each individual error handler.

  13. #13
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    Thanks TMS! That worked exactly like you said it would!

    As a bonus, I also learned that I can add other steps/ tasks in the ErrorHandler; very nice!

    I'll mark this as solved.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: How to Exit Sub if called function has error?

    You're welcome. Thanks for the rep.

  15. #15
    Forum Contributor
    Join Date
    07-22-2009
    Location
    Minneapolis, MN
    MS-Off Ver
    2016
    Posts
    220

    Re: How to Exit Sub if called function has error?

    Right now the error handler at the bottom of each macro & function calls a macro that generates a pop up window with the details of the error, then automatically generates me an email with the same information, along with a hyperlink of the filepath.

    I appreciate the suggestion, and will keep that in mind if/ when I go through and revise how everything fits together. I have this thread bookmarked for future reference.

    Thanks!

+ 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] How to exit out of a moduel grcefuly if it is called from another moduel?
    By redsab in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-02-2014, 06:09 AM
  2. [SOLVED] Quick beginner question-Why doesn't On Error Exit Function work?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-09-2013, 06:23 AM
  3. Error When Macro is Called From Another Sheet
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2011, 08:29 AM
  4. [SOLVED] Excel error when called from vb script
    By Paul Davidson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2006, 04:10 PM
  5. [SOLVED] Excel error when called from vb script
    By Paul Davidson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2006, 04:00 PM

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