0 votes
in VBA by Beginner (123 points)

There is no statement in the code labelled 0. So What does GoTo 0 mean?

1 Answer

+1 vote
by Beginner (186 points)
selected by
 
Best answer

Dear sandeep kothari,

     The full syntax of the On Error statement is:

On Error Goto Label
On Error Goto 0
On Error Goto -1
On Error Resume Next 

where Label is an identifier that marks a certain block of code within the function body. About the On Error Goto 0 statement the official Microsoft documentation says:

On Error GoTo 0 disables error handling in the current procedure. It doesn't specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0. Without an On Error GoTo 0 statement, an error handler is automatically disabled when a procedure is exited.

As you said, even if there isn't a block of code labelled 0, this instruction tells the interpreter to disable an error handler previously defined in a function's body. So you should be aware that:

  1. The On Error GoTo 0 statement disables any error handler declared in the body of the function (if any, otherwise it does nothing).
  2. Any error handler within the body of the function is disabled at the exit from the function, even if there is no On Error GoTo 0 statement.
  3. The On Error GoTo 0 statement doesn't affect any statements before it. Yo can think of it a a closing bracket for a try { ... } catch { ... } statement in Java Script. It limits the scope (the sequence of statements) on which an error handler acts.

Ryan Wells gives and example of such a statement when checking a file for existence. I'll post it here:

Function FileExists(FilePath As String) As Boolean
    Dim TestStr As String
    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        FileExists = False
    Else
        FileExists = True
    End If
End Function

In the body of the FileExists function an error handler has been set up by On Error Resume Next, meaning that if an exception is raised within the body of that function, excecution will resume at statement immediatlery after the one that raised the exception. By using On Error GoTo 0, we disable that handler, meaning that if an exception is raised by the code following the On Error GoTo 0 statement, then the exception will be passed down on the stack, because in the function body there isn't any active error handler. So the On Error GoTo 0 statement limit the scope of the error handler to just TestStr = Dir(FilePath). Ryan Wells has done that so that if the Dir() function raises an exception, the Resum Next catches it.

In Python the code would have looked like:

def file_exists(path):
    teststr = ""
    try:
        teststr = dir(path)
    except Exception:
        pass
    if teststr == "":
        return False
    else:
        return True

where dir() is a fictional function with the same meaning as Dir(). Here except has the same function as On Error GoTo 0, i.e. to confine the actions of the error handling code to the statements between try and except. (There are better ways of testing a file for existence in Python)

by Beginner (123 points)
Thanks for your detailed answer.

Let's stick to VBA.

ON ERROR GoTo 0

I remember in BASIC code, we used GoTo 0 to make the code execute line no. 0 in the code. But in VBA, ON ERROR GoTo 0 does not seem to act similarly. Please clarify.
by Beginner (186 points)

I'll try to clarify my point even more: the On Error GoTo 0 statement marks the point within the body of a function where all previous error handlers are disabled. It has nothing to do with jumping to a instruction labelled by 0. Generally, the GoTo statement jumps to the label, but in this particular case the doesn't stand for a label. If it helps you, you may think of it as an instruction called Disable Handlers

by Beginner (123 points)
Is it Disable Handlers or should it be Enable Error Handler?
by Beginner (186 points)

No, it is Disable Handlers. Let me put it other words. Suppose that in the body of a function you put a statement like On Error GoTo ErrorHandler (where ErrorHandler is a label you define somewhere else in the body of the function, containing the code you want to use to handle the exception). This error handler will act on all the code from it to the end of the function body. You may want to limit it to just a few istructions, e.g. because the other instructions won't raise any exception. That's where On Error GoTo 0 come in handy. It basically says: all the previous error handlers in the body of this function are disabled. That's what Ryan Wells does in his example: since the If ... Else statement at the bottom of the function body won't raise any exception, it limits the scope of the On Error Resume Next handler to just TestStr = Dir(FilePath). Let me know if you have any doubts about that.

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

What makes us different?
Our points system rewards you with a chance for free gifts as your contributions grow. The more points you earn, the better the prizes.

Getting Started
Register
Prizes

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

Top Users Mar 2019
  1. YasserKhalil

    306 Points

  2. ParserMonster

    204 Points

  3. Alberto Semat

    186 Points

  4. danmcg

    127 Points

  5. Abdan

    124 Points


Prizes for March 2019
Terms and Conditions
1st place (1) 1-year MyExcelOnline Academy Membership
(2) My First Add-in Dev Pack & Course
(3) AutoMacro: VBA Code Generator (Developer)
(4) VBA Cheat Sheet Bundle
(5) $35 Amazon Gift Card
2nd place (1) My First Add-in Dev Pack & Course
(2) 101 Most Popular Excel Formulas E-Book
(3) VBA Cheat Sheet Bundle
(4) $25 Amazon Gift Card
3rd place (1) Mouse to Macro
(2) VBA Cheat Sheet Bundle
(3) $15 Amazon Gift Card
...