30 Advanced Excel Tips for 2026

Excel Champs / VBA / VBA GoTo Statement
By Puneet Gogia (Microsoft MVP)

VBA GoTo Statement

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

GoTo jumps to a label in the same procedure. Write the label on its own line as a name followed by a colon, then send the code there with GoTo and the name:

Sub CheckValue()
    If Range("A1").Value = "" Then GoTo NoData
    MsgBox "Value: " & Range("A1").Value
    Exit Sub
NoData:
    MsgBox "A1 is empty"
End Sub

The label must be in the same Sub or Function; GoTo can’t jump into another procedure, so call that procedure by name instead. Put Exit Sub before a label you only want to reach by jumping, or the code runs into it.

Most jumps are clearer written another way: Exit Sub to stop early, Do ... Loop to repeat, and If ... Else to choose. The main use that remains is error handling with On Error GoTo.

A GoTo that jumps back to repeat a question needs a way out. If an InputBox loop accepts only the right answer, clicking Cancel returns an empty string and the question repeats forever, so test If answer = "" Then Exit Sub first. Application.Goto is a different thing: it selects a cell or range, as in Application.Goto Worksheets("Data").Range("A1").

The VBA GoTo statement helps code execution jump to a specific line within the procedure. In simple words, with the GoTo statement, VBA jumps to a particular line that you specify with a label (or a line number). For example, if you put a label just before the second line and tell GoTo to jump to it, VBA will jump to that line.

How to Use VBA GoTo Statement in a Code

  1. First, you need to use the GoTo statement.
  2. After that, you need to define the place where you want VBA to jump from GoTo.
  3. Next, create a label to mark that place in the procedure (the label name followed by a colon, like Last:).
  4. In the end, add the line(s) of code that you want to get executed.
vba-go-to-statement-in-code
Sub vba_goto()
GoTo Last
Range("A1").Select
Last:
Range("A12").Select
End Sub

In this code, GoTo Last makes VBA skip the line that selects A1 and jump straight to the Last: label, so it selects A12. A label has to start at the beginning of a line, its name must start with a letter, and it ends with a colon. And the label has to be in the same procedure as the GoTo statement.

GoTo to Repeat a Code

You can also use the GoTo statement to repeat a code using a set of conditions.

Sub goto_repeat() 

Dim iMessage As String
' Declares a variable to store the user's input as a string.

Question:
' This is a label used for the GoTo statement to reference.

iMessage = InputBox("What's the day today?")
' Displays an input box prompting the user to input the current day.
' The input is stored in the iMessage variable.

If iMessage <> "tuesday" Then
' Checks if the user's input is not equal to "tuesday".

MsgBox "Wrong answer, try again."
' If the input is not "tuesday", this message box is shown to inform the user.

GoTo Question
' Sends the program back to the 'Question' label, prompting the user to try again.

Else
' If the input is equal to "tuesday", the following block of code runs.

MsgBox "That's the right answer."
' Displays a message box indicating the correct answer was given.

End If
' End of the If-Else condition.

End Sub

When you run the above code, it asks you for the day’s name, and if you enter anything other than “tuesday” (in lowercase), it will repeat the question. If you look at the code, you can see we have used the GoTo and if statements.

Now, when the user enters anything other than the right answer, the GoTo statement makes VBA repeat the code. (But whenever you use these sorts of codes, you need to be extra cautious, as they can create an endless loop.)

There are two things to know about this code. First, the comparison is case-sensitive, so “Tuesday” with a capital T counts as a wrong answer. Second, if the user clicks Cancel, InputBox returns an empty string, and the question comes back again and again. The version below fixes both: LCase turns the answer into lowercase before comparing, and an empty answer exits the code.

Sub goto_repeat_safe()
Dim iMessage As String
Question:
iMessage = InputBox("What's the day today?")
If iMessage = "" Then Exit Sub
If LCase(iMessage) <> "tuesday" Then
MsgBox "Wrong answer, try again."
GoTo Question
End If
MsgBox "That's the right answer."
End Sub

You can also write the same thing with a Do loop instead of GoTo. Microsoft recommends loops and IF statements wherever you can use them, as code with too many GoTo statements gets hard to read and debug.

Sub repeat_with_loop()
Dim iMessage As String
Do
iMessage = InputBox("What's the day today?")
If iMessage = "" Then Exit Sub
Loop Until LCase(iMessage) = "tuesday"
MsgBox "That's the right answer."
End Sub

VBA GoTo End

You can also exit a sub using the GoTo statement.

vba-go-to-end

In the above code, we have used the “Last” label just before the End Sub statement. When VBA reaches the GoTo statement, it jumps to the “Last” label at the end of the procedure.

But if all you want is to stop the procedure, Exit Sub does the same job in one line without a label. GoTo to the end is useful when you have some lines there that must always run, like turning screen updating back on.

Sub vba_exit_sub()
If Range("A1").Value = "" Then Exit Sub
MsgBox "A1 has a value."
End Sub

GoTo Statement to Go to a Line in Another Sub Routine

Well, the GoTo statement is quite handy for making VBA jump from one place to another, but it’s not possible to make it go to another procedure in the module.

If you try, VBA shows the compile error “Label not defined”. To run code from another procedure, call that procedure instead.

Sub first_macro()
MsgBox "Running the first macro."
Call second_macro
End Sub

Sub second_macro()
MsgBox "Now running the second macro."
End Sub

On Error GoTo for Error Handling

The most common use of GoTo in real code is with On Error. It tells VBA to jump to a label when an error occurs, so you can show a clear message instead of the VBA error box.

Sub vba_on_error_goto()
On Error GoTo ErrorHandler
Range("A1").Value = 10 / Range("B1").Value
Exit Sub
ErrorHandler:
MsgBox "Something went wrong: " & Err.Description
End Sub

In this code, if B1 is zero or blank, the division gives an error, and VBA jumps to ErrorHandler. The Exit Sub line just before the label makes sure that the message only shows up when there’s an error.

Application.Goto to Go to a Cell or a Range

Don’t confuse the GoTo statement with the Application.Goto method. The GoTo statement jumps to a line in your code, and Application.Goto takes you to a cell or a range in the workbook. If the range is on another sheet, it activates that sheet first, and with Scroll:=True, it scrolls the window so that the cell shows up at the top left.

Sub vba_application_goto()
Application.Goto Reference:=Worksheets("Sheet1").Range("A154"), Scroll:=True
End Sub

Frequently Asked Questions

Can I use a line number with GoTo?

Yes. GoTo can jump to a line label or a line number. To use a number, start the line with it (like 10 MsgBox “Done”) and write GoTo 10. Labels with names are easier to understand, so most people use them.

Why do I get the error “Label not defined”?

The label name after GoTo doesn’t match any label in the same procedure. Check the spelling, make sure the label ends with a colon, and make sure it’s in the same Sub or Function, not in another one.

Is it bad to use GoTo in VBA?

Not always. On Error GoTo is the normal way to handle errors, and a jump to a clean-up section at the end is fine. But for repeating code or making decisions, a loop, an IF statement or Select Case is easier to read and debug.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel