Skip to main content

GoTo statement

Table of contents
  1. Syntax
  2. Remarks
  3. Example

Branches unconditionally to a specified line within a procedure.


GoTo line

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


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


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

Sub GotoStatementDemo()
    Dim Number, MyString
    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.
    MyString = "Number equals 2"
    Debug.Print MyString ' Print "Number equals 1" in
    ' the Immediate window.
End Sub

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>