Error handling Resume v Goto

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,208
According to this article by Chip Pearson, you should not use GoTo in error handling.

Error Handling In VBA

For example:

Code:
Dim MyArray(1 To 10) As VariantDim Counter As Integer
On Error GoTo Errhandler
For Counter = 1 To 20
    MyArray(Counter) = Counter
Next Counter
Cont:
    MsgBox "Finished"
Exit Sub
Errhandler:
    MsgBox "Error"
    
    GoTo Cont ' SHOULD USE RESUME Cont

I do agree with him as I have come across an example where using GoTo did not achieve the desired result (although the example above does work with GoTo).

I could have added an error reset like this (and in examples that didn't work without it, it still didn't work with it):

Code:
Dim MyArray(1 To 10) As Variant Dim Counter As Integer
On Error GoTo Errhandler
For Counter = 1 To 20
    MyArray(Counter) = Counter
Next Counter
Cont:
    MsgBox "Finished"
Exit Sub
Errhandler:
    MsgBox "Error"
    
    On Error Goto 0 ' Error reset

    GoTo Cont ' SHOULD USE RESUME Cont

What Chip Pearson did not say is what actually happens if you did use Goto instead of Resume.

Does anybody know?

Thanks
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,177
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
If you don't use Resume, you don't clear the exception that was raised when the error occurs and any further errors will not be handled. On Error Goto 0 does not do this either. See my blog here: On Error WTF? | Excel Matters
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,208
If you don't use Resume, you don't clear the exception that was raised when the error occurs and any further errors will not be handled. On Error Goto 0 does not do this either. See my blog here: On Error WTF? | Excel Matters
Thanks for your artcile but I don't think I fully understand one point.

Rich (BB code):
The only ways to reset an active error condition and deactivate an error handler are via a Resume, Exit Sub, Exit Function, or Exit Property statement, or via an On Error Goto -1 statement.
Further on, you say:

Rich (BB code):
If you find yourself using On Error Goto -1 a lot (or at all), you need to stop and rethink what you are doing! I have never, ever, seen well-written code that required it and have never used it myself in actual production code.
Doesn't your first quote state using On Error GoTo -1 is a valid way of resetting the error?

I added the line:

Rich (BB code):
On Error GoTo -1
to an example of mine (not the one shown above) that originally failed and it now works.
 
Last edited:

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,177
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Yes it does say that it's a valid way. Just as using Goto or GoSub are valid ways of doing things; or simply putting On Error Resume Next at the top of your code. That does not mean that they should be your first thought. There are generally better ways of achieving the same ends (which is one reason I suspect you don't see On Error Goto -1 either documented or used very often).
 

Watch MrExcel Video

Forum statistics

Threads
1,102,859
Messages
5,489,309
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