Why this nested error handler works?

telesien

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

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,177
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
35
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
35,177
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,858
Messages
5,489,306
Members
407,686
Latest member
Chuck1960

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top