Error handling Resume v Goto

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,825
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
 
Upvote 0
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:
Upvote 0
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).
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top