GoSub (statement)

Syntax

GoSub label

Description

Causes execution to continue at the specified label.

Comments

Execution can later be returned to the statement following the GoSub by using the Return statement.

The label parameter must be a label within the current function or subroutine. GoSub outside the context of the current function or subroutine is not allowed.

Example

This example gets a name from the user and then branches to a subroutine to check the input. If the user clicks Cancel or enters a blank name, the program terminates; otherwise, the name is set to MICHAEL, and a message is displayed.

Sub Main()
  uname$ = Ucase$(InputBox$("Enter your name:","Enter Name"))
  GoSub CheckName
  MsgBox "I'm looking for MICHAEL, not " & uname$
  Exit Sub

CheckName:
  If (uname$ = "") Then
    GoSub BlankName
  ElseIf uname$ = "MICHAEL" Then
    GoSub RightName
  Else
    GoSub OtherName
  End If
  Return

BlankName:
  MsgBox "No name? Clicked Cancel? I'm shutting down."
  Exit Sub
RightName:
  Msgbox "Hey, MIKE where have you been?"
  End
OtherName:
  Return
End Sub

See Also

Goto (statement); Return (statement).

More information

G