Question about "On Error"

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
946
Office Version
  1. 2016
Platform
  1. Windows
Hello All.
I have three modules, that I've combined into one, so all will run sequentially.
One of the three modules has: "On Error Resume Next"
The other two modules have: "On Error GoTo 0"

Should / can I have both, if so, does the code go at the very top, and taken out of the below modules that has been combined to the original top module.
Thanks for the help
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Putting "On Error Resume Next" at the top of the code is (IMO) about the worst thing you can possibly do in VBA. It simply ignores any errors encountered (without you knowing), which can cause lot's of problems & makes it almost impossible to debug code.
 
Upvote 0
does the code go at the very top
On Error Resume Next disables error handling, so one would never be aware whether an error occurs in run-time. Bad idea.
On Error GoTo 0 enables the previous enabled error handler. That can be a custom one, or VBA's default error handler.
 
Upvote 0
On Error GoTo 0 enables the previous enabled error handler.
Not sure about that, I thought it simply disables any active error handling.
 
Upvote 0
On Error GoTo 0 enables the previous enabled error handler. That can be a custom one, or VBA's default error handler.
Not sure about that, I thought it simply disables any active error handling.

Nope, try this ...

VBA Code:
Sub One()
    Dim errcount As Long
    On Error Resume Next
    Two
    On Error GoTo CustomHandler
    Two
    Three
    Exit Sub
CustomHandler:
    errcount = errcount + 1
    MsgBox "Ouch " & errcount
    Err.Clear
    Resume Next
End Sub

Sub Two()
    Dim i: i = 1 / 0
End Sub

Sub Three()
    On Error Resume Next
    i = 1 / 0
    On Error GoTo 0     ' <<<<
    i = 1 / 0
    MsgBox "This is never displayed"
End Sub
 
Upvote 0
so the reason why I have On Error Resume Next, is simply because the data set I paste into the macro, has areas where it divides by zero in certain rows. So I want it to skip and continue. I just don't know if I need both?
Thanks for the help
 
Upvote 0
Interesting, it works differently when running across modules, because if you use
VBA Code:
Sub One()
Dim i
    Dim errcount As Long
    On Error Resume Next
    i = 1 / 0
    On Error GoTo CustomHandler
    i = 1 / 0
    On Error Resume Next
    i = 1 / 0
    On Error GoTo 0     ' <<<<
    i = 1 / 0
    MsgBox "This is never displayed"

    Exit Sub
CustomHandler:
    errcount = errcount + 1
    MsgBox "Ouch " & errcount
    Err.Clear
    Resume Next
End Sub
You only goto CustomHandler once
 
Upvote 0
the data set I paste into the macro, has areas where it divides by zero in certain rows.
It would be better to check for that, rather than using On Error Resume Next. But if you do use, follow it with On Error Goto 0 as soon as possible (ie with a line or two of code)
 
Upvote 0
@Fluff This all makes sense, since the documentation reads: "On Error GoTo 0 - Disables any enabled error handler in the current procedure."
 
Upvote 0
That does make perfect sense, I had forgotten about that last little bit, but then I very rarely use error handling.
 
Upvote 0

Forum statistics

Threads
1,214,806
Messages
6,121,672
Members
449,045
Latest member
Marcus05

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