Visual Basic for Applications Reference

Visual Studio 6.0

On...GoSub, On...GoTo Statements

See Also    Example    Specifics

Branch to one of several specified lines, depending on the value of an expression.

Syntax

On expression GoSub destinationlist

On expression GoTo destinationlist

The On...GoSub and On...GoTo statement syntax has these parts:

Part Description
expression Required. Any numeric expression that evaluates to a whole number between 0 and 255, inclusive. If expression is any number other than a whole number, it is rounded before it is evaluated.
destinationlist Required. List of line numbers or line labels separated by commas.

Remarks

The value of expression determines which line is branched to in destinationlist. If the value of expression is less than 1 or greater than the number of items in the list, one of the following results occurs:

If expression is Then
Equal to 0 Control drops to the statement following On...GoSub or On...GoTo.
Greater than number of items in list Control drops to the statement following On...GoSub or On...GoTo.
Negative An error occurs.
Greater than 255 An error occurs.

You can mix line numbers and line labels in the same list. You can use as many line labels and line numbers as you like with On...GoSub and On...GoTo. However, if you use more labels or numbers than fit on a single line, you must use the line-continuation character to continue the logical line onto the next physical line.

Tip   Select Case provides a more structured and flexible way to perform multiple branching.

Show: