Now, if you press the F8 key once code execution jumps to the next line, and the active line task will be executed. Now run the code line by line to see the impact by pressing the F8 key. So after the first worksheet code adds the error disable line On Error GoTo 0. We need the error to occur if there is no worksheet “Sheet2”. In this example, we just need to ignore the error for the first worksheet but for the second sheet onwards.
Since we have added On Error Resume Next at the top, it has started to handle the error, but at the same time, we need to specify how many lines we need to ignore this error. Imagine the scenario where we need to ignore the error in case of non-availability of the worksheet “Sheet1,” but we need to notify if there is no worksheet called “Sheet2”. It won’t give any error messages because the error handler statement On Error Resume Next is enabled. To handle this error, I will add an error handler statement “On Error Resume Next” at the top of the macro. Since there is no sheet named as “Sheet1”, it has encountered a “Subscript out of range” error. When we run the code, it will encounter an error like the below. There are no sheets called “Sheet1” & “Sheet2”. Now I have the below sheets in my workbook. Then it will select the worksheet named “Sheet2” and insert the same value.
What the above code does is it will first select the worksheet named “Sheet1,” and in cell A1 it will insert the value 100.
Source: VBA On Error Goto 0 () How to use On Error GoTo 0 Statement? You are free to use this image on your website, templates etc, Please provide us with an attribution link How to Provide Attribution? Article Link to be Hyperlinked We need to use the statement “On Error GoTo 0”.Īny line code written between these statements will ignore any kind of error that occurred in the proceedings. To handle the error, we need to start with the statement “On Error Resume Next” and to end this error handler. VBA programming language too, and we also have error handling techniques in this programming language.“On Error Resume Next” enables the error handler, and “On Error GoTo 0” will disable the enabled error handler.īoth “On Error Resume Next” & “On Error GoTo 0” are pairs that need to be used in tandem for the efficiency of the code. It is referred to as “Error Handler Disabler”.Įrror handling in any of the programming languages is a master class that all the coders need to understand. VBA On Error GoTo 0 is an error handler statement used to disable the enabled error handler in the procedure.