In Excel VBA, GoTo transfers execution to a named label elsewhere in the same procedure. It is most useful as part of an error handler—written On Error GoTo label—and is usually best avoided for ordinary program flow, where If, loops, and procedure calls are easier to follow.
What GoTo means in Excel VBA
GoTo is a VBA control-flow statement. A procedure runs from one line to the next unless a condition, loop, procedure call, or jump changes that path. A GoTo statement sends VBA directly to a label in the same Sub or Function. The label marks the destination; it does not create a separate procedure.
For example, GoTo Finish jumps to a line labelled Finish:. VBA does not search the workbook for that name: the label must exist in the current procedure. Labels are not case-sensitive, but choosing a descriptive name helps readers understand the destination.
This is different from Excel’s worksheet Go To command. In a worksheet, Ctrl+G on Windows opens the Go To dialog, where you can move to a cell or named range. That command does not run VBA. In the Visual Basic Editor (VBE), GoTo is code that changes execution flow.
Syntax and labels
The basic form uses a label name followed by a colon on its own line. The jump and its destination must be within the same procedure. A label can be placed before ordinary statements, but making it visually distinct is generally clearer.
GoTo LabelName
' Other statements may appear here.
LabelName:
' Execution continues from this point.
Here, LabelName is an example, not a reserved VBA word. Use a name that describes what happens next, such as HandleError or CleanUp. A missing label, a misspelled label, or a jump to a label in another procedure causes a compile error. A label does not accept arguments and cannot be called like a Sub.
VBA also permits numeric line labels, but named labels are usually more readable. Avoid relying on old-fashioned numbered lines unless maintaining code that already uses them. The related statement On Error GoTo LabelName has special error-handling behavior: it activates an error handler rather than immediately jumping to the label.
A small GoTo example
This short macro demonstrates an unconditional jump over a message. In the VBE, press F8 on Windows to step through code one line at a time. On a Mac, the equivalent may be Fn+F8, depending on the keyboard and function-key settings.
Sub ShowGoToExample()
Dim messageText As String
messageText = "The jump worked."
GoTo ShowResult
' This line is skipped.
messageText = "This text is never assigned."
ShowResult:
MsgBox messageText
End Sub
Before running a macro, save a copy of the workbook: macro actions cannot be undone with Ctrl+Z. This example is deliberately simple to show the jump; it is not a recommendation to use GoTo to skip routine statements. In normal code, an If...Else structure would usually explain the choice more directly.
To view or edit VBA in Excel for Microsoft 365 on Windows, use Developer > Visual Basic. If the Developer tab is hidden, enable it in File > Options > Customize Ribbon. On Mac, use the Developer tab’s Visual Basic command if available; keyboard shortcuts can depend on the Mac’s function-key settings. Excel for the web can open workbooks containing macros, but it cannot create, edit, or run VBA macros.
Using On Error GoTo for error handling
The most established use of a GoTo label in VBA is directing an error to a handler. The following example tries to find a worksheet and reports a useful message if the sheet is missing or the assignment fails.
Sub UpdateReportTime()
Dim reportSheet As Worksheet
On Error GoTo HandleError
Set reportSheet = ThisWorkbook.Worksheets("Summary")
reportSheet.Range("A1").Value = Now
Exit Sub
HandleError:
MsgBox "Update failed (" & Err.Number & "): " & _
Err.Description, vbExclamation
End Sub
The line On Error GoTo HandleError tells VBA where to transfer control if a run-time error occurs in the procedure. If the worksheet exists, the macro writes the current date and time to cell A1, then Exit Sub prevents normal execution from falling through into the handler. If an error occurs first, VBA runs the handler and displays the error number and description.
Save a copy of the workbook before running this macro too; VBA changes are not reversed by Ctrl+Z. An error handler is not a substitute for checking expected conditions. If a worksheet may or may not exist, you can deliberately test for it and give a clear result instead of treating every missing sheet as an unexpected failure. Keep the handler focused on reporting or safely recovering from errors, not silently hiding them.
On Error GoTo 0 disables the current procedure’s enabled error handler. On Error Resume Next instead tells VBA to continue with the next statement after an error; use it narrowly, and check Err promptly, or a real problem can be overlooked. These are distinct from GoTo 0: the latter is an ordinary jump only if a label named 0 were in scope, while On Error GoTo 0 is a special error-control instruction.
When to use GoTo—and when not to
For most decisions, use If...Then...Else. For repeated work, use For, For Each, or Do loops. For reusable tasks, put the work in a separate Sub or Function. These constructs show the shape of the program, making it easier to tell what runs and when.
A GoTo jump may be reasonable when a short, controlled transfer makes the code simpler—for example, moving to one shared cleanup section in a procedure with several failure paths. But a cleanup label should not become a maze. If many jumps cross large sections of code, consider splitting the procedure into smaller procedures or using structured conditions instead.
- Use
On Error GoTo HandlerNamewhen a procedure needs a defined response to run-time errors. - Use
If...Elsefor choices such as “if the value is valid, process it; otherwise, report a problem.” - Use loop statements to repeat work rather than jumping backward to imitate a loop.
- Use a separate procedure when a block of work has a clear purpose and can be called directly.
There is no performance reason to choose GoTo for typical Excel automation. Readability and correct error handling matter more than shaving a few lines. A short macro that makes its path obvious is generally safer to modify than one that depends on readers tracing several jumps.
Common mistakes and troubleshooting
- Confusing the two forms:
GoTo LabelNamejumps immediately.On Error GoTo LabelNamesets an error handler that runs only if a qualifying run-time error occurs. - Putting the label in the wrong place: the destination must be in the same procedure as the jump. A label in another
Subcannot be reached this way. - Forgetting to exit before a handler: if normal execution reaches the handler label, it runs like ordinary code. Put
Exit SuborExit Functionbefore a handler when normal execution should bypass it. - Leaving an error handler active: an active handler can affect later statements in that procedure. Use
On Error GoTo 0when you want to disable it after the relevant operation. - Using Resume Next too broadly: it can make a macro appear to succeed even when a key operation failed. Restrict it to a small section and inspect the error state before continuing.
- Expecting Ctrl+Z to reverse a macro: Excel’s undo history does not undo VBA macro actions. Save a separate copy before running code that changes data, formatting, or workbook structure.
If VBA highlights a GoTo line or label, use Debug > Compile VBAProject in the VBE to identify compile problems, such as a missing destination label. If the code compiles but the handler does not appear to run, check whether the error occurs in the procedure where the handler is set, whether another error-handling statement changed the active behavior, and whether the code actually encountered a run-time error.
Frequently Asked Questions
What does GoTo do in Excel VBA?GoTo moves execution to a named label in the same procedure. It does not move the worksheet selection or open Excel’s Go To dialog.
What is the difference between GoTo and On Error GoTo?GoTo Label jumps to the label as soon as VBA reaches that statement. On Error GoTo Label sets up an error handler and transfers control there if a run-time error occurs.
Can GoTo jump to another Sub or Function?
No. A GoTo destination must be a label within the same procedure. To run code in another procedure, call that Sub or Function instead.
How do I stop an error handler from running after successful code?
Place Exit Sub or Exit Function before the handler label when successful execution should leave the procedure. Otherwise normal execution could continue into the handler’s statements.
Is GoTo bad practice in VBA?
Not categorically. It is common and appropriate for error handlers, but excessive jumps make control flow difficult to follow. Prefer structured conditions and loops for ordinary decisions and repetition.
Related
- How to Insert a Functioning Checkbox in Microsoft Word
- How to Insert a Gray Line in Word: Shapes and Borders
- How to Insert a Handwritten Signature in Word
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Module: How It Works, Types, and When to Use OneAn Excel VBA module is a container for Visual Basic for Applications code, such as macros, helper procedures,…
- VBA Dictionary Key Count: How to Use the Count PropertyTo count the keys in a VBA dictionary, read its Count property: dict.Count. It returns the number of…
- VBA Range.Delete: Remove Cells, Rows, or ColumnsIn Excel VBA, use the Delete method to remove cells, rows, or columns: for example, Range("B2:C5").Delete Shift:=xlShiftUp deletes…
- VBA Range.Item: How Cell Indexing WorksIn Excel VBA, Range.Item(rowIndex, columnIndex) returns a Range object for the cell at the specified row and column…
- Google Sheets Macros: How They Work and What to KnowYes. Google Sheets supports macros: you can record repeated actions and run them again, or edit the generated…
- Excel UserForm Combo Box: Add, Populate, and Use ItAn Excel UserForm combo box is a VBA control that lets someone choose an item from a drop-down…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.