On Error GoTo 0

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,825
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The code below works fine.

Code:
Dim i As Integer
    
    For i = 1 To 100
    
        Dim a As Integer
        
        If i > 1 Then a = 1 / 0
        
        On Error GoTo abc
        
            a = i / 0
        
cde:
    
    Next i

    Exit Sub

abc:
    
    'On Error GoTo 0
    
    Resume cde

Notice I have commented out the line:

Code:
    On Error GoTo 0

I understand why it fails it is was added in but is it good practice to always add On Error GoTo 0?

If I were to add it in, I would put it here:

Code:
Dim i As Integer
    
    For i = 1 To 100
    
        Dim a As Integer
        
        If i > 1 Then a = 1 / 0
        
        On Error GoTo abc
        
            a = i / 0
        
        On Error GoTo 0
        
cde:
    
    Next I

    Exit Sub

abc:
    
    'On Error GoTo 0
    
    Resume cde
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
It's going to reset your other On Error statement, so where you put it depends on what you want to happen but, with the code you have posted, it would never be executed so it's pretty pointless.
 
Upvote 0
I see.

Would this be better?

Code:
Dim i As Integer
    
    For i = 1 To 100
    
        Dim a As Integer
        
        On Error GoTo abc
        
            If i > 1 Then a = 1 / 0
            
            a = i / 0
            
cde:
    
        On Error GoTo 0
    
    Next I

    Exit Sub

abc:
    
    Resume cde
 
Last edited:
Upvote 0
Again, it depends entirely on what you want to achieve.
 
Upvote 0
Just to reset error handling after activating it.

My main concern is I have code that calls the code I've posted, so I think it makes sense to "clean up" along the way rather than leave things to build up.
 
Last edited:
Upvote 0
Error handling is specific to a routine. There is absolutely no need to add On Error Goto 0 to a routine that is about to end anyway.
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

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