GoTo Statement

Branches unconditionally to a specified line.

Syntax

GoTo line

The required line argument can be any line label or line number.

Remarks

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

GoTo can also be used in the global area of visibility.

    Note: Too many GoTo statements can make code difficult to read and debug. Use structured control statements (Do...Loop, For...Next, If...Then...Else, Select Case) whenever possible.

Example

Sub GotoDemo()
Dim Number As Double, MyString

Number = InputBox("Enter number :") ' Initialize variable.

' Evaluate Number and branch to appropriate label.
If Number = 1 Then
GoTo Line1
Else GoTo Line2
End If

Line1:
MyString = "Number equals 1"
GoTo LastLine ' Go to LastLine.

Line2:
MyString = "Number equals " & Number

LastLine:
Trace MyString

End Sub

 

See Also

Do...Loop Statement, For...Next Statement, GoSub...Return Statement, If...Then...Else Statement, Select Case Statement