Author: Oscar Cronquist Article last updated on November 06, 2018

The image above demonstrates the GoTo statement. It "jumps" or "navigates" to another place in the code, in this case, "Start".

VBA code

Sub HowToGoTo()

a = 1
Start:

    MsgBox Range("B2:B4").Cells(a)
    If a = 3 Then Exit Sub
    a = a + 1

GoTo Start

End Sub

Explaining subroutine

  1. The subroutine begins with variable a setting it equal to 3.
  2. Start: is a label which the GoTo statement use in order to know where to "jump".
  3. The message box appears and shows the value in cell range B2:B4 based on what variable a contains.
  4. The IF THEN statement checks if variable a is equal to 3 and exits the subroutine if the condition is met.
  5. Variable a is added with number 1.
  6. The GoTo statement makes the code "jump" to label "Start:"
  7. The subroutine is repeated until a is equal to 3.

The GoTo statement is mostly used in error handling techniques. I recommend using For Next, Do While or Do Loop since they make your code easier to follow and understand.