Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: Why this nested error handler works?

  1. #1
    New Member
    Join Date
    May 2016
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Why this nested error handler works?

    Hi, I tried to nest error handler within an error handler, which as I found out is not really possible.

    However I did manage to find solution, which made it work, but without explanation how and why. I don't want to blindly use codes, even though they do work for me.

    Can someone please explain to me, why this workaround works?

    Code:
    On Error GoTo Errorhandler
    *code
    
    Errorhandler:
    *code
    
    Resume Error
    
    Error:
    On Error GoTo Errorhandler2
    The key lines are the red ones. Without them it would be just nesting error handler into error handler, which doesn't work and you get the usual VBA error message instead of progressing to second error handler.

  2. #2
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    32,351
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Why this nested error handler works?

    The Resume statement resets the current exception state, which is what then allows you to set a new error handler correctly. See here: On Error WTF? | Excel Matters

  3. #3
    New Member
    Join Date
    May 2016
    Posts
    32
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Why this nested error handler works?

    Ah, so "on error" is more of a on/off switch than simple redirect. Now it makes sense. As you can see I lack any formal coding or programming background, so I have to work with analogies

    I guess that while the above mentioned code works, it is probably way too messy solution or professionals.

    Thanks for the answer

  4. #4
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    32,351
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    3 Thread(s)

    Default Re: Why this nested error handler works?

    No, On Error is a redirect. But once you have raised an error, you have to reset that error state before any other errors can be handled. To do that, you want a Resume statement.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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