Table of contents
TOC
Collapse the table of content
Expand the table of content

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

office 365 dev account|Last Updated: 6/12/2017
|
1 Contributor

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

Syntax

OnexpressionGoSubdestinationlist

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

PartDescription
expressionRequired. 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.
destinationlistRequired. List of line numbers orline 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 isThen
Equal to 0Control drops to the statement following On...GoSub or On...GoTo.
Greater than number of items in listControl drops to the statement following On...GoSub or On...GoTo.
NegativeAn error occurs.
Greater than 255An 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 theline-continuation character to continue the logical line onto the next physical line.

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

Example

This example uses the On...GoSub and On...GoTo statements to branch to subroutines and line labels, respectively.

Sub OnGosubGotoDemo() 
Dim Number, MyString 
 Number = 2 ' Initialize variable. 
 ' Branch to Sub2. 
 On Number GoSub Sub1, Sub2 ' Execution resumes here after 
 ' On...GoSub. 
 On Number GoTo Line1, Line2 ' Branch to Line2. 
 ' Execution does not resume here after On...GoTo. 
 Exit Sub 
Sub1: 
 MyString = "In Sub1" : Return 
Sub2: 
 MyString = "In Sub2" : Return 
Line1: 
 MyString = "In Line1" 
Line2: 
 MyString = "In Line2" 
End Sub
© 2017 Microsoft