Why this nested error handler works?

telesien

New Member
Joined
May 25, 2016
Messages
33
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

[COLOR=#ff0000]Resume Error

Error:[/COLOR]
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,178
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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
 
Last edited:

telesien

New Member
Joined
May 25, 2016
Messages
33
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 :)
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,178
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
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.
 

Forum statistics

Threads
1,078,252
Messages
5,339,100
Members
399,278
Latest member
randomNumberGenerator2211

Some videos you may like

This Week's Hot Topics

Top