Excel Macro GoTo: VBA Labels, Ranges, and Examples

VBA & macros · Updated November 13, 2026

In an Excel macro, GoTo can mean either VBA’s GoTo label statement, which jumps to a named line in the same procedure, or Application.Goto, which navigates to a cell or named range. Use the first to change a macro’s execution path and the second to select or display a worksheet location.

What GoTo means in Excel VBA

“Go To” is also the name of Excel’s navigation command, so the phrase can refer to three different things. The VBA statement, the VBA method, and Excel’s user-interface command are related by purpose but are not interchangeable.

  • GoTo LabelName changes where VBA continues running inside the current procedure.
  • Application.Goto activates the relevant workbook and worksheet, then selects a cell or range.
  • Excel’s Go To dialog moves the selection when a person is working in the worksheet; it does not itself add a command to a macro.

Understanding which one you need prevents a common mistake: writing GoTo "A1" when the intention is to select cell A1. A cell address is not a VBA line label. Use Application.Goto for a cell address, or use the Go To dialog manually.

How the GoTo statement works

A VBA label is a name followed by a colon, on its own line. The statement GoTo followed by that label transfers execution to the labeled line. The label must be in the same Sub or Function as the statement; it cannot be used to jump into a different procedure or module.

For example, this macro jumps over the reorder message when the stock quantity is sufficient:

Sub CheckInventory()
    Dim qty As Long

    qty = 12
    If qty >= 10 Then GoTo InStock
    MsgBox "Reorder this item."
    Exit Sub

InStock:
    MsgBox "Stock level is sufficient."
End Sub

With qty set to 12, the condition is true and VBA continues at InStock:, displaying “Stock level is sufficient.” If the condition is false, it displays the reorder message and then exits the procedure. The Exit Sub matters: without it, execution would continue to the label and display both messages.

A label is not a variable, a worksheet name, or a cell reference. It marks a position in the procedure. Labels are often written at the left edge of the code window for readability, and a colon is required after the label name.

Use Application.Goto to navigate to a cell

When a macro should take the user to a cell or named range, use Excel’s Application.Goto method. Its Reference argument identifies the destination. The optional Scroll argument tells Excel whether to scroll so the destination is visible near the top-left of the window.

This example selects cell A1 on the worksheet named Summary in the workbook containing the macro:

Sub ShowSummaryStart()
    Dim ws As Worksheet
    Dim target As Range

    ' Set the destination on this workbook's Summary sheet.
    Set ws = ThisWorkbook.Worksheets("Summary")
    Set target = ws.Range("A1")

    Application.Goto Reference:=target, Scroll:=True
End Sub

Here, ThisWorkbook means the workbook that contains the VBA code. That is usually safer than ActiveWorkbook, which refers to whichever workbook is currently active and could change while the macro runs. The worksheet name must match an actual sheet tab in that workbook. If the sheet is named differently, update the text in the code.

Application.Goto can also use a defined name as its reference, provided the name exists and is in scope. It is useful for navigation buttons, summary macros, or routines that should leave the user at the result after processing. It does not mean “go to a VBA label.”

How to add and run a GoTo macro

In Excel for Microsoft 365 on Windows, the basic workflow is to open the Visual Basic Editor, add a standard module, enter the procedure, and run it. Before running any macro, save a copy of the workbook: macro actions generally cannot be reversed with Ctrl+Z.

  1. If the Developer tab is not visible, choose File > Options > Customize Ribbon, select Developer, and choose OK.
  2. Choose Developer > Visual Basic, or press Alt+F11, to open the editor.
  3. In the editor, choose Insert > Module, then paste or type the procedure into the module window.
  4. Return to Excel and run it from Developer > Macros, or place the cursor inside the procedure in the editor and use its Run command.

On a Mac, Excel also supports VBA macros, but keyboard shortcuts can vary with the keyboard and function-key settings. Use Developer > Visual Basic to open the editor if available; the Windows Alt+F11 shortcut may require an Fn key or may differ. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros. Use the desktop Excel application for these steps.

Common uses and a safer alternative to repeated jumps

GoTo is most defensible for a small, clearly named branch or for directing VBA to an error-handling section. Repeated jumps in many directions can make code difficult to trace, because readers have to track where every branch leads.

For ordinary decision-making, an If...Then...Else block often communicates the logic more clearly. For repeated work, a For or Do loop is usually easier to maintain than jumping backward to repeat a section. For example, a loop can process each row in a range without creating a label and manually returning to it. Use a descriptive label such as HandleError rather than a vague name such as Again, so the destination’s purpose is apparent.

Do not use GoTo to simulate a worksheet formula or to jump between procedures. Put reusable work in a separate Sub or Function and call it by name. That keeps navigation, business logic, and error handling distinct.

Using GoTo for error handling

VBA’s conventional error-handler pattern uses On Error GoTo followed by a label. If a runtime error occurs after the handler is enabled, VBA transfers execution to the labeled section. A normal completion path should exit the procedure before the handler label; otherwise, VBA can fall through into the handler even though no error occurred.

Sub HighlightActiveCell()
    Dim selectedCell As Range

    On Error GoTo HandleError
    Set selectedCell = ActiveCell
    selectedCell.Interior.Color = RGB(255, 255, 0)
    Exit Sub

HandleError:
    MsgBox "The cell could not be highlighted: " & Err.Description, vbExclamation
End Sub

The macro attempts to color the active cell yellow. If that operation raises an error, execution goes to HandleError: and shows the error description. This simple handler reports the problem; it does not repair every possible cause. In a larger macro, a handler may also need to close files, restore application settings, or release resources before exiting.

On Error GoTo 0 disables an enabled error handler in the current procedure. That is different from On Error GoTo HandleError, which directs errors to a label, and from On Error Resume Next, which tells VBA to continue after an error. Avoid using Resume Next as a blanket way to hide errors; it can allow a macro to keep working with incomplete or incorrect results.

Excel’s Go To dialog is not a VBA label

If you only want to navigate manually, Excel provides the Go To dialog. On Windows, press Ctrl+G or F5, type a cell reference such as D250, and press Enter. You can also use Home > Find & Select > Go To. To select a named range, enter its name in the dialog instead.

On Mac, Control+G opens Go To in many Excel keyboard configurations; the function-key equivalent can depend on system settings. If a shortcut does not work, use Excel’s menu command. This dialog changes the current selection, whereas GoTo LabelName changes the next line of VBA that runs. Typing a VBA label into the dialog will not cause a macro to jump to that label.

Common problems and fixes

  • “Label not defined”: Check the spelling of the label in both places, including the colon on the label line. Labels are procedure-specific, so a label in another Sub is not a valid destination.
  • The macro shows two messages: A path may be falling through to the label after completing its first task. Add an appropriate Exit Sub or restructure the logic with If...Then...Else.
  • The cell destination is not found: Check the sheet tab name, workbook reference, and cell address. A missing sheet can cause an error before Application.Goto runs.
  • The macro will not run: Confirm that you are using desktop Excel, that the workbook is macro-enabled when it needs to store VBA, and that macro security settings permit the file to run. Do not enable macros in an untrusted workbook just to test a snippet.
  • Ctrl+Z does not undo the macro: Excel generally clears its undo history when VBA changes workbook content. Save a separate copy first and test on sample data.

When the editor highlights a line after an error, read the exact error message and check the destination or object used on that line. A problem with Worksheets("Summary"), for example, is different from an undefined VBA label. Keeping those two meanings of “GoTo” separate makes troubleshooting faster.

Frequently Asked Questions

How do I write GoTo in an Excel macro?
Write GoTo LabelName and define LabelName: in the same procedure. Use this to move execution to a line of VBA, not to select a worksheet cell.

How do I make a macro go to cell A1?
Use Application.Goto with a qualified range, such as the A1 range on the intended worksheet. Qualifying the worksheet and workbook helps avoid navigating to the wrong open file.

Can GoTo jump to another Sub or worksheet?
No. The VBA GoTo statement targets a label in the same procedure. To run another Sub, call it by name; to navigate to a worksheet range, use Application.Goto.

Is GoTo bad practice in VBA?
Not always. It is common in a straightforward error-handler pattern and can be reasonable for a small branch. Many tangled jumps make code harder to understand, so use structured conditions and loops for routine logic.

Can I run an Excel VBA macro in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in the desktop version of Excel to create, edit, or run its VBA code.

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.