On Error GoTo 0

Thanks Thanks:  0
Likes Likes:  0
Results 1 to 7 of 7

Thread: On Error GoTo 0

  1. #1
    Board Regular
    Join Date
    Apr 2013
    Posts
    573
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default On Error GoTo 0

     
    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 by tiredofit; Jul 17th, 2017 at 05:13 AM.

  2. #2
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    30,511
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo 0

    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.

  3. #3
    Board Regular
    Join Date
    Apr 2013
    Posts
    573
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo 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 by tiredofit; Jul 17th, 2017 at 07:59 AM.

  4. #4
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    30,511
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo 0

    Again, it depends entirely on what you want to achieve.

  5. #5
    Board Regular
    Join Date
    Apr 2013
    Posts
    573
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo 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 by tiredofit; Jul 17th, 2017 at 08:44 AM.

  6. #6
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    30,511
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo 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.

  7. #7
    Board Regular
    Join Date
    Apr 2013
    Posts
    573
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo 0

      
    Thanks

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com