Excel VBA GoTo: How Labels and Errors Work

VBA & macros · Updated November 21, 2026

In Excel VBA, GoTo is a statement—not a function—that transfers execution to a labeled line in the same procedure. It is also used in the error-handling pattern On Error GoTo Label; for selecting or navigating to a cell, the separate Application.Goto method may be what you mean.

What GoTo Means in VBA

A VBA procedure normally runs from top to bottom, with statements such as If, For, and Select Case controlling which instructions run. GoTo changes that flow by sending VBA to a label elsewhere in the current procedure. A label is an identifier followed by a colon, such as SkipRow:. It does not return a value, accept function-style arguments, or perform a calculation.

Here is the basic form:

GoTo LabelName

' Other statements may appear here.

LabelName:
' Execution continues here.

The label must be in the same Sub or Function as the GoTo. You cannot use it to jump into another procedure. A label can be placed on a line by itself or before a statement. Although line numbers can also be used in some VBA contexts, named labels are easier to read and maintain.

The phrase “Go To” can refer to different Excel features, so it helps to distinguish them: the VBA GoTo statement changes program flow; On Error GoTo directs execution when an error occurs; and Excel’s Go To command navigates around a worksheet. They are related in name, but they are not interchangeable.

How a GoTo Label Works

VBA processes the procedure until it reaches a GoTo statement, then continues at the matching label. For example, a macro can skip a few instructions when a condition is true. This can be useful for a short, deliberate jump, but a sequence of jumps can make it difficult to understand which statements run and in what order.

This example marks rows with a nonblank value in column A. It jumps over the marking instruction when the cell is blank. Before running any macro that changes a workbook, save a separate copy: VBA changes cannot be undone with Ctrl+Z (or Command+Z on Mac).

Sub MarkNonblankRows()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowNum As Long

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    For rowNum = 2 To lastRow
        If Len(Trim$(CStr(ws.Cells(rowNum, 1).Value))) = 0 Then GoTo NextRow
        ws.Cells(rowNum, 2).Value = "Checked"
NextRow:
    Next rowNum
End Sub

The code assumes a worksheet named Sheet1, a header in row 1, and values to inspect in column A. It writes “Checked” in column B for each nonblank value and leaves blank rows alone. If the sheet name does not exist, the macro raises an error. A more structured version could put the marking instruction inside an If block instead, avoiding the jump.

Using On Error GoTo for Error Handling

One of the most common reasons to see GoTo in real VBA is error handling. The statement On Error GoTo ErrorHandler tells VBA to transfer control to the named error-handling section if a run-time error occurs in that procedure. The label is still an ordinary procedure label; the On Error statement is what gives it its error-handling role.

For example, the following procedure deliberately divides by zero so the handler can display the error message. In useful code, replace that operation with work that could genuinely fail, and provide recovery or cleanup appropriate to the task.

Sub DemonstrateErrorHandler()
    Dim numerator As Double
    Dim denominator As Double
    Dim result As Double

    On Error GoTo ErrorHandler

    numerator = 10
    denominator = 0
    result = numerator / denominator
    MsgBox result
    Exit Sub

ErrorHandler:
    MsgBox "The operation failed: " & Err.Description, vbExclamation
End Sub

The Exit Sub before the handler matters: if the calculation succeeds, it prevents normal execution from falling through into the error-handling section. In a Function, use Exit Function instead. An error handler should generally explain the problem, record useful details, or restore a safe state—not silently hide the error and continue as if the operation succeeded.

On Error GoTo 0 disables the active error handler in the current procedure and restores normal VBA error behavior. It is different from GoTo 0, which is a normal jump to a label named 0 only in contexts where such a label is valid; it is not the syntax for turning off error handling. VBA also supports On Error GoTo -1 to clear the current procedure’s error state. These are error-handling controls, not general-purpose destinations for ordinary flow.

Application.Goto Is a Different Feature

If your goal is to select a cell, range, or defined name, use Excel’s Application.Goto method rather than the VBA GoTo statement. The method can activate the relevant sheet and optionally scroll the window so the reference is visible. It is a method call, not a VBA function for branching to a code label.

For example, this macro selects cell A1 on a worksheet named Sheet1 and scrolls it into view. Save a copy of the workbook before running macros; even a navigation macro can affect the active selection and view, while other macros may make changes that Ctrl+Z cannot reverse.

Sub NavigateToA1()
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Application.Goto Reference:=ws.Range("A1"), Scroll:=True
End Sub

The method can also be used with a defined name or a reference supplied as text, provided that reference is valid. The Scroll argument is optional. If you mean Excel’s worksheet navigation command instead, press Ctrl+G or F5 on Windows to open Go To. On Mac, the available shortcut can depend on keyboard settings; use Excel’s Edit menu or search for Go To in the ribbon interface if the Windows shortcut does not apply.

When to Choose If, Select Case, or a Loop

For most ordinary branching, structured statements make intent clearer than jumping to a label. They show the relationship between a condition and the actions it controls, and they are easier to modify without accidentally bypassing important code.

  • Use If...Then...Else when a condition chooses between a small number of paths, such as handling a blank cell differently from a filled one.
  • Use Select Case when one expression can match several distinct values, such as a status field with “New,” “Open,” and “Closed” cases.
  • Use For...Next or Do...Loop when the task repeats for a known range or until a condition is met.
  • Use a separate Sub or Function when a block of work deserves a clear name or needs to be reused.
  • Reserve GoTo for a short, readable jump or conventional error handling when a structured alternative would be less clear.

The row-marking example can be written without a jump by wrapping the action in an If block. That style makes the rule explicit: perform the write only when the cell is not blank. Neither approach changes the result, but a simple conditional is generally easier for a future maintainer to follow than a label used solely to skip one line.

Common GoTo Problems

A frequent mistake is misspelling the label or changing its name in one place but not the other. The label and the destination must match. A label also has to be in the same procedure; putting it in another Sub does not make it reachable. If VBA highlights the GoTo line during compilation, check the spelling, punctuation, and location of the label.

Another problem is placing a label in a confusing position. A jump transfers control directly to the label, so any statements between the jump and the label are skipped. Conversely, if normal execution reaches a label while moving down through the procedure, it continues there like any other line. Use an Exit Sub or Exit Function before a handler or other section that should only run after a jump, as in the error-handling example.

Labels are procedure-level destinations, but VBA imposes restrictions on jumps into the interior of structured blocks such as loops. Do not use GoTo to enter a loop or conditional block from outside it. Keep labels in straightforward positions, and prefer normal loop and conditional structure when moving between parts of a routine. This avoids brittle control flow and errors caused by bypassing initialization or cleanup code.

Finally, remember that an error handler only handles run-time errors that occur while it is enabled. It does not fix compile errors, such as undeclared or misspelled identifiers, and it does not automatically make a failed operation safe to repeat. Test macros on a copy, especially when they edit many cells, delete data, or save files.

Where to Enter and Run VBA

In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. To add a standard module, use Insert > Module in the editor, then paste the procedure into that module. If the Developer tab is not visible in Excel, enable it through File > Options > Customize Ribbon and select Developer. Run a procedure from the editor with the cursor inside it by pressing F5, or return to Excel and use Developer > Macros.

On Mac, Excel also supports VBA, though some features and shortcuts differ. The Visual Basic Editor can be opened from the Developer tab, and the keyboard shortcut may require the Fn key depending on the Mac’s function-key settings. Excel for the web can open a workbook containing macros, but it cannot create, edit, or run VBA macros; use the desktop Excel app for those tasks.

When saving a workbook that contains VBA, use an Excel Macro-Enabled Workbook file, with the .xlsm extension. A standard .xlsx file does not preserve VBA project code when saved in that format. Only enable or run macros from sources you trust, and keep an unmodified backup when the code could change or remove data.

Frequently Asked Questions

Is GoTo a function in Excel VBA?
No. GoTo is a VBA statement that transfers execution to a label in the same procedure. Application.Goto is a separate Excel method used to navigate to a cell, range, or name.

How do I write GoTo in VBA?
Write GoTo LabelName and create a matching label such as LabelName: elsewhere in the same Sub or Function. Keep the jump simple and make sure normal execution cannot accidentally run a section intended only for the jump.

What does On Error GoTo 0 do?
It disables the current procedure’s enabled error handler, so later run-time errors follow VBA’s normal error behavior. It is not a command to jump to a worksheet cell or to a label called zero.

Can GoTo jump to another Sub or worksheet?
No. The VBA statement cannot transfer execution into another procedure. To run another routine, call that Sub or Function; to navigate to a worksheet cell, use a worksheet reference or Application.Goto.

Can I undo a VBA GoTo macro with Ctrl+Z?
No. Excel’s undo history does not generally reverse changes made by VBA macros. Save a separate copy before running a macro that edits workbook data, and test it on sample data first.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.