Goto (statement)


Goto label


Transfers execution to the line containing the specified label.


The compiler will produce an error if label does not exist.

The label must appear within the same subroutine or function as the Goto.

Labels are identifiers that follow these rules:

  1. Must begin with a letter.

  2. May contain letters, digits, and the underscore character.

  3.  Must not exceed 80 characters in length.

  4.  Must be followed by a colon (:).

Labels are not case-sensitive.


This example gets a name from the user and then branches to a statement, depending on the input name. If the name is not MICHAEL, it is reset to MICHAEL unless it is null or the user clicks Cancel--in which case, the program displays a message and terminates.

Sub Main()
  uname$ = UCase(InputBox("Enter your name:","Enter Name"))
  If uname$ = "MICHAEL" Then
    Goto RightName
    Goto WrongName
  End If

  If (uname$ = "") Then
    MsgBox "No name? Clicked Cancel? I'm shutting down."
    MsgBox "I am renaming you MICHAEL!"
    uname$ = "MICHAEL"
    Goto RightName
  End If
  Exit Sub

  MsgBox "Hello, " & uname$
End Sub

See Also

GoSub (statement); Call (statement).


To break out of an infinite loop, press Ctrl+Break.

More information