The "Label not defined" error in Excel VBA occurs when the compiler encounters a line with a line label (like MyLabel:) but cannot find where that label is defined elsewhere in the procedure. This is a compile-time error—meaning Excel won’t even try to run your code—so it must be fixed before execution. The fix is straightforward: either define the missing label or remove the invalid GoTo or Resume statement referencing it.
What You'll Need
- Microsoft Excel installed (any version from 2010 onward supports this)
- Basic familiarity with VBA (Visual Basic for Applications) and the Visual Basic Editor (VBE)
- A workbook with VBA code that triggers the "Label not defined" error
Step-by-Step
- Open your Excel workbook and press Alt + F11 to launch the Visual Basic Editor (VBE). If the VBE window doesn’t appear, ensure Excel isn’t minimized or hidden behind other windows.
- In the VBE, locate the problematic procedure. Look in the Project Explorer pane (press Ctrl + R if it’s hidden) for your workbook, then expand VBAProject (YourWorkbookName) > Modules or ThisWorkbook or Sheet1 (Sheet Name), depending on where your code resides.
- Double-click the module or object containing the code to open it in the code window. Scroll through the code to find lines like
GoTo MyLabel,Resume MyLabel, orResume Nextfollowed by an undefined label reference. - Search for the label name (e.g.,
MyLabel:) in the same procedure. A valid label is a name followed by a colon (:) on its own line, with no leading spaces or indentation (e.g.,MyLabel:must be flush left or only preceded by a single space if used withExit Sub). If the label is missing, you must add it. - To add the label, insert a new line where you want execution to resume (e.g., after error handling or a conditional branch). Type the exact label name followed by a colon, like
ErrorHandler:orSkipCheck:. Ensure the spelling matches *exactly*—including case—because VBA treats labels as case-sensitive during compilation. - If the label exists but is outside the current procedure (e.g., in a different Sub or Function), move the
GoToorResumestatement inside the same procedure, or rewrite the logic using a function call or boolean flag instead. VBA labels only work *within* a single procedure. - After correcting the label or reference, press Ctrl + S to save, then return to Excel. Press Alt + F8, select your macro, and click Run to verify the error is gone.
Common Mistakes
- Spelling inconsistencies: Writing
GoTo ErrorHandlerbut definingErrorhandler:(lowercase h)—VBA will flag this as undefined. - Indentation errors: Placing the label on a line with leading spaces or tabs. Labels must be at the start of a line (no indentation), or VBA may misinterpret them as part of a statement.
- Scope confusion: Placing a label inside one procedure (e.g.,
Sub A) and trying toGoToit from another (e.g.,Sub B). Labels have procedure-level scope only. - Using labels after
Exit SuborExit Functionwithout a matching entry point—this breaks control flow and often triggers the error if combined with error handling. - Copying code between procedures without updating label names—especially common when reusing snippets from online forums.
Tips
- Use descriptive labels like
InvalidInput:orLogError:to improve readability and reduce spelling errors. - When writing error handlers, keep the pattern consistent:
On Error GoTo ErrorHandlerat the top,Exit Subbefore the label, thenErrorHandler:with cleanup code. This avoids accidental jumps into error code. - Consider avoiding
GoToentirely for new code. Modern alternatives likeIf...Thenblocks, loops, or structured error handling (On Error GoTo 0andErr.Raise) are easier to debug. - Enable Require Variable Declaration in the VBE (Tools > Options > Editor tab > check the box). While it doesn’t fix label errors directly, it reduces other syntax issues that can mask problems.
- Use the Debug > Compile VBAProject menu after changes. This forces immediate feedback on label mismatches before running the macro.
Troubleshooting
If the error persists after adding the label, follow these checks:
- Verify the label is in the same procedure: Open the code window, click the dropdown at the top-left (showing "(General)") and select the procedure name. Ensure the label line appears in the same list item.
- Check for hidden characters: Copy the label name, paste it into Notepad, and compare spelling. Sometimes invisible characters (like non-breaking spaces) sneak in when pasting from web pages. Retype the label manually.
- Confirm no duplicate labels: A procedure can’t have two labels with the same name (e.g., two
ErrorHandler:lines). Use Ctrl + F to search the procedure and remove duplicates. - Test a minimal example: Create a new module and paste this short block:
Sub TestLabel() On Error GoTo MyLabel MsgBox "Starting" Exit Sub MyLabel: MsgBox "Error occurred" End SubRun it. If this works, the issue is in your original code’s structure—likely a misplacedGoToor missingEnd Sub. - Check for corrupted modules: Right-click the module in the Project Explorer, choose Export File, then delete the module. Create a new module, import the file, and recompile. Corruption can break label recognition.
- Restart Excel: Rarely, VBA’s internal symbol table gets stuck. Close Excel completely (check Task Manager for lingering
EXCEL.EXEprocesses), then reopen and test again.
Frequently Asked Questions
Can I use a label in one procedure and jump to it from another?
No. Labels in VBA are local to the procedure (Sub or Function) where they’re defined. You cannot useGoTo across procedures. Use a separate Sub or Function instead, and call it with Call MyProcedure.
Why does VBA say "Label not defined" even though I see the label in the code?
Most likely, the label has a typo, extra spaces, or is accidentally placed inside a string or comment. Example:' GoTo MyLabel (commented out) won’t cause the error, but if your GoTo line says GoTo MyLabe (missing the l), it fails. Compare both lines character by character.
Is there a keyboard shortcut to find all labels in a procedure?
No built-in shortcut exists, but in the VBE, press Ctrl + F, enter: in the Find what box, and click Find Next. This highlights every label line in the current module. Filter by procedure using the dropdowns at the top of the code window.
Do labels work in Excel 365 and Excel 2021?
Yes. Label-based flow control (likeGoTo) is fully supported in all modern Excel versions that include VBA (including Microsoft 365 subscriptions and Excel 2021). This is a core VBA feature, not version-specific.
Can I use labels with error handling like On Error Resume Next?
Yes, but they serve different purposes. On Error Resume Next skips errors silently; GoTo jumps to a specific label. You might combine them: use On Error GoTo ErrorHandler for structured error handling, then define ErrorHandler: to log and exit cleanly.
What happens if I delete a label but forget to remove the GoTo line?
This directly causes the "Label not defined" error. Always search for the label name (Ctrl + F in the module) before deleting it—and double-check all GoTo, Resume, and Resume Next statements that reference it.