GoTo Statement

Description

Branches unconditionally to a specified line within a procedure.

Syntax

GoTo line

The line argument can be any line label or line number.

Remarks

GoTo can branch only to lines within the procedure where it appears.

Note Too many GoTo statements can be difficult to read and debug. Use structured control statements (Do...Loop, For...Next, If...Then...Else, Select Case) whenever possible.

See Also

Do...Loop Statement, For...Next Statement, GoSub...Return Statement, If...Then...Else Statement, Select Case Statement.

Example

This example uses the GoTo statement to branch to line labels within a procedure.


Sub GotoStatementDemo()
    Number = 1                 ' Initialize variable.
                            ' Evaluate Number and branch to appropriate                             ' label.
    If Number = 1 Then GoTo Line1 Else GoTo Line2
    :
    MyString = "Number equals 1"
    GoTo LastLine            ' Go to LastLine.:
    ' The following statement never gets executed.
    MyString = "Number equals 2": 
    Debug.Print MyString        ' Print "Number equals 1" in
                            ' Debug window.Sub