GoTo Statement

See Also2I1H6SV              Example3HAXCW>Low

Branches unconditionally to a specified line within a procedure.


GoTolinelabel | linenumber}


The GoTo statement has these parts:

Part                        Description


GoTo                       Causes program execution to continue at another place within a procedure.

linelabel                   Label that marks the line to execute next.  The linelabel must begin with an alphabetic character, must end with a colon, must be 40 characters or less, and must not be a Visual Basic keywordJLVHLD.  Each linelabel must be unique in the module in which it is used.  Line labels are not case-sensitive and may begin in any column as long as they are the first nonblank characters on the line.

linenumber               Line number that marks the line to execute next.  The linenumber can be any number of 40 characters or less, can contain only decimal digits (0-9), and must not end with a colon.  Each linenumber must be unique in the module in which it appears.  Line numbers18F50XF may begin in any column as long as they are the first nonblank characters.  If linenumber is greater than 65,529, the Erl function cannot properly report the line number where an error has occurred.

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


Note   Programs with 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 StatementLANDO

For...Next StatementLANFOR

GoSub...Return Statement3HAWIA

If...Then...Else StatementLANIF

Select Case StatementVCC4T3

GoTo Statement Example

The example uses GoTo to branch around a subroutine.  This kind of branching is generally considered to be poor programming technique.  To avoid this kind of branching, turn subroutines into Sub procedures.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim Num                         ' Declare variable.

   Num = InputBox("Type a number.")

   GoSub Routine                   ' Branch to subroutine.

   GoTo Nextpart                   ' Branch around subroutine.

Routine:                           ' Start of subroutine.

   Num = Num / 2

   Return                          ' Return from subroutine.

Nextpart:                          ' Continuation of program.

   Msgbox "Half of your number is " & Num

End Sub