GoTo Statement
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

GoTo Statement

Branches unconditionally to a specified line within a procedure.

GoTo line


Required. Any line label.


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

You cannot use a GoTo to branch from outside a For...Next, For Each...Next, SyncLock...End SyncLock, Try...Catch...Finally, or With...End With block to a label inside.

Within a Try...Catch...Finally construction, you cannot use GoTo to branch out of a Try block, into a Catch block, or into or out of a Finally block. You can branch from a Catch block into the Try block associated with that Catch. For example, if one Try...Catch...Finally construction is nested within another, a Catch block can branch into the Try block at its own nesting level, but not into any other Try block.

Note   GoTo statements can make code difficult to read and maintain. Whenever possible, use Do...Loop, For...Next, If...Then...Else, Select Case, Try...Catch...Finally, While, and With...End With structures instead.


This example uses the GoTo statement to branch to line labels within a procedure.

Sub GotoStatementDemo()
Dim Number As Integer
dim MyString As String
   Number = 1   ' Initialize variable.
   ' Evaluate Number and branch to appropriate label.
   If Number = 1 Then GoTo Line1 Else GoTo Line2
   MyString = "Number equals 1"
   GoTo LastLine   ' Go to LastLine.
   ' The following statement never gets executed because Number = 1.
   MyString = "Number equals 2"
   ' Print "Number equals 1" in the Output window.
   Debug.WriteLine (MyString)  
End Sub

See Also

Do...Loop Statements | For...Next Statements | If...Then...Else Statements | Select...Case Statements | Try...Catch...Finally Statements

© 2015 Microsoft