On Error GoTo, On Error Resume Next, and GoTo 0 in Excel VBA – Code Included

11
11



On Error enables error-handling within your macro and specifies what will be done in an error occurs.

On Error GoTo – sends the code to a specific line

On Error Resume Next – just continues to the next line if an error occurs. And there is no Debug window if there is a runtime error.

On Error GoTo 0 – Resets the error handler and will show the Debug form if an error occurs.

Code:
================
Sub ErrorMod()

On Error GoTo errHandler

d = 0

‘On Error Resume Next

‘On Error GoTo 0

If 0 / d = True Then
MsgBox “You did it”
End If

Exit Sub

errHandler:

Debug.Print Err.Description
‘MsgBox “Error”

d = 1

‘Resume – Will go back to the line of code that the error occured on and try to run it again

‘Resume

‘Resume Next – Will continue the Code but go to the next line
Resume Next

End Sub

Nguồn: https://blockchainvietnam.vn

Xem thêm bài viết khác: https://blockchainvietnam.vn/tong-hop/

11 COMMENTS

  1. HI,
    Thanks for this wonderful video. One question please – if in VBA query first I have used On error resume next because i wanted to ignore the errors on top and then later on at some point down If error occurs I need to goto xxxxxx: for that i used this on error goto xxxx: so do you think it will be fine. Or it will still go to first resume next and then come to this second error part. request your answer please

  2. Thank you sir for this video.
    But I still have some doubt on "on error goto 0". I am not able to make a difference when i am simpally running my code and when i am running with the "on error goto 0". In both cases i am getting the same output.
    my code is :-
    sub fun
    on error goto 0
    if 0/0 = true then
    msgbox "keshav singh"
    end if
    end sub

    fun

  3. Thank you for this video, this is exactly what Im working on for my project.
    If after the error handler you expect another error, can you create another error handler?
    Thank you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here