VBA GoTo

The GoTo statement is used to jump to a location in code within the current procedure specified by a label or line number. GoTo is also used for handling errors. The GoTo statement is necessary for error handling, but should generally not be used otherwise. There are more secure and structured alternatives to using GoTo.

On Error GoTo

The GoTo statement can be used to control error handling behavior. GoTo can be used to jump to an error handling block of code using a label or line number, clear the current error handling scope, or reset an error handling trap after an error has occurred.

Statement Description
On Error GoTo {Label|Line} Jumps to label or line number when an error occurs
On Error GoTo 0 Clears the Err object and clears the error handling scope
On Error GoTo -1 Clears the Err object and resets the error trap
Public Sub Example()

    On Error GoTo HandleError

    Err.Raise 1004 'Error handled

    Debug.Print "Resuming..."

    On Error GoTo 0

    Err.Raise 1004 'Error not handled

    Exit Sub

HandleError:
    Debug.Print "Handling Error..."
    Resume Next

End Sub
Public Sub Example()

    On Error GoTo HandleError1

    Err.Raise 1004 'Error handled

    Exit Sub

HandleError1:
    Debug.Print "HandleError1"
    On Error GoTo -1
    On Error GoTo HandleError2
    Err.Raise 1004 'Error handled
    Exit Sub

HandleError2:
    Debug.Print "HandleError2" 
    Exit Sub

End Sub

GoTo Label

The GoTo statement can be used to jump to an arbitrary location inside a procedure specified by a label.

Public Sub Example()

    Debug.Print "Hello, World!"

    GoTo MyLabel

    Debug.Print "Howdy"

MyLabel:
    Debug.Print "MyLabel reached."

End Sub

One common use for GoTo is to jump out of loops under certain conditions. This goal can always be accomplished another way. In the example below, a boolean flag can be used to determine when to exit the outer loop instead of using GoTo to jump out of the loop.

Public Sub ExampleWithGoTo()

    Dim i As Long
    Dim j As Long

    For i = 1 To 3
        For j = 1 To 3
            If j > 2 Then
                GoTo JumpOutOfLoops
            Else
                Debug.Print i, j
            End If
        Next j
    Next i
JumpOutOfLoops:

    Debug.Print "Done"

End Sub
Public Sub ExampleWithoutGoTo()

    Dim i As Long
    Dim j As Long

    Dim MyFlag As Boolean
    MyFlag = False
    
    For i = 1 To 3
        For j = 1 To 3
            If j > 2 Then
                MyFlag = True
                Exit For
            Else
                Debug.Print i, j
            End If
        Next j
        If MyFlag Then
            Exit For
        End If
    Next i

    Debug.Print "Done"

End Sub

GoTo Line Number

GoTo can be used to jump to a specific line of code specified by a line number.

Public Sub Example()
10    Debug.Print "Hello, World!"
20    GoTo 40
30    Debug.Print "Howdy"
40    Debug.Print "Line Reached."
End Sub

On...GoTo

On...GoTo can be used to jump to labels or line numbers based on the result of a numeric expression that evaluates to a number between 0 and 255. The Select Case statement can be used instead of On...GoTo.

Syntax: On expression GoTo sub1, sub2, ...

Public Sub Example()

    Dim Num As Byte
    Num = 2

    On Num GoTo Label1, Label2, Label3

ExitSub:
    Debug.Print "Exiting Sub"
    Exit Sub

Label1:
    Debug.Print "Label1"
    GoTo ExitSub

Label2:
    Debug.Print "Label2"
    GoTo ExitSub

Label3:
    Debug.Print "Label3"
    GoTo ExitSub

End Sub
Public Sub Example()

    Dim Num As Byte
    Num = 2

    On Num GoTo 10, 20, 30

ExitSub:
    Debug.Print "Exiting Sub"
    Exit Sub

10  Debug.Print "Line10"
    GoTo ExitSub

20  Debug.Print "Line20"
    GoTo ExitSub

30  Debug.Print "Line30"
    GoTo ExitSub

End Sub