Excel VBA’s Label not defined compile error means a procedure refers to a jump target that VBA cannot find. Check each GoTo, GoSub, On Error GoTo, or Resume reference in the procedure, then add the missing label or correct the reference so the spelling matches.
What the error means
A VBA label is a named point in a procedure. Code can transfer control to that point using a statement such as GoTo Retry. The destination must exist in the same Sub, Function, or Property procedure. If VBA sees a jump to Retry but finds no matching Retry: label there, it cannot compile the procedure.
For example, this code refers to a label that is absent:
Public Sub CalculateTotal()
GoTo Finished
Debug.Print "Calculating"
End Sub
Adding a label with the same name resolves that missing reference:
Public Sub CalculateTotal()
GoTo Finished
Debug.Print "Calculating"
Finished:
Debug.Print "Finished"
End Sub
In practice, it is usually better to avoid a jump if ordinary structured code will do. The example is intentionally small to show the relationship between the reference and its destination.
How VBA labels and jumps work
A label is written as a name followed by a colon, on a line within a procedure. A label is not a variable, a worksheet name, or a bookmark shared across the workbook. VBA resolves a jump to a label within the procedure containing that jump; placing the label in another Sub or module does not satisfy the reference.
VBA does not distinguish upper- and lowercase letters in identifiers, so GoTo ErrorHandler and ErrorHandler: refer to the same label. Spelling and punctuation still matter: ErrorHandler is not the same as ErrorHandle, and a label’s colon is part of its declaration syntax. VBA also supports numbered line labels, though named labels are generally easier to understand and maintain.
The error can come from ordinary control flow or error handling. For example, On Error GoTo HandleError tells VBA to transfer execution to a label called HandleError if a trappable runtime error occurs. That handler label must also be present in the same procedure. A misspelled handler name can therefore produce this compile error even if the code that might fail has not run.
Find the missing reference in the editor
Open the Visual Basic Editor from Excel for Microsoft 365 on Windows with Alt+F11, or select Developer > Visual Basic. On a Mac, use Developer > Visual Basic; the keyboard shortcut can vary with the keyboard and function-key settings. The Excel for the web app cannot create, edit, or compile VBA macros, so use the desktop app for these steps.
In the Visual Basic Editor, the error dialog usually highlights the statement that refers to the missing label. Read the whole statement rather than assuming the highlighted word itself is the only problem. Then search the current procedure for a matching label, including labels near the end of the code. If the editor’s selection is unclear, close the dialog and inspect every jump reference in that procedure.
To check the project again after editing, choose Debug > Compile VBAProject in the Visual Basic Editor. The command may be unavailable when there is nothing new to compile. If another error appears, fix that one and compile again; a project can contain more than one compile problem, and VBA generally reports them one at a time.
Fix it step by step
- Identify the procedure. Note the Sub, Function, or Property containing the highlighted statement. A target elsewhere in the project will not work.
- Read the target name. For
GoTo Cleanup, the intended label isCleanup:. ForOn Error GoTo ErrHandler, the intended handler isErrHandler:. - Search within that procedure. Look for a label with the same spelling. Check for a deleted label, a typo, or a label that was renamed without updating its references.
- Choose the intended repair. Restore the label if the jump is needed, correct the reference if its name is wrong, or remove the jump if it is obsolete and the remaining logic is sound.
- Compile again. Use Debug > Compile VBAProject to verify that VBA can compile the project. Then run the relevant macro with a safe test case.
Do not add a label merely to silence the message if you do not know what the jump should do. A label changes the route through the procedure. Sending execution to the wrong place can skip initialization, cleanup, or other required statements, creating a logic bug even though the compile error is gone.
Common causes and examples
- A spelling mismatch:
GoTo CleanUprefers to a different name fromCleanup:. Although case does not matter, the letters do. - A removed or renamed label: Someone changes a procedure but leaves an older
GoToor error-handler statement behind. - A label in another procedure: A label named
ExitPoint:in a different Sub cannot be the target of the current Sub’sGoTo ExitPoint. - A missing error-handler destination:
On Error GoTo ErrHandlerremains in the code, but theErrHandler:section was deleted or renamed. - A mistaken assumption about special values:
On Error GoTo 0is a special instruction that disables the current procedure’s enabled error handler; it does not mean VBA should find a label named zero. Do not change it to a named label unless you intend to install a handler.
One source of confusion is that VBA has more than one kind of statement involving “Go To.” The Visual Basic Editor’s Edit > Find can help locate text in code, while Excel’s Find & Select > Go To or the Ctrl+G shortcut moves around a worksheet. Neither worksheet navigation nor a sheet tab name creates a VBA label. The compile message concerns code references, not cell addresses or worksheet names.
Writing a clear error handler
A typical error-handling structure includes a normal exit before the handler. Without that exit, a procedure that reaches the handler label during ordinary execution may run the handler even though no error occurred. The following pattern includes the required label and separates normal completion from error handling:
Public Sub ExampleProcedure()
Dim total As Long
On Error GoTo ErrHandler
total = 10 \ 2
Debug.Print total
Exit Sub
ErrHandler:
Debug.Print Err.Description
End Sub
Here, ErrHandler: matches the target in On Error GoTo ErrHandler, and Exit Sub prevents normal execution from falling through into the handler. The calculation prints 5; the handler runs only if a trappable error occurs before the procedure exits. The example demonstrates the structure, not a need to add error handling to every short macro.
Before running a macro that changes data, save a copy of the workbook. Macro actions generally cannot be undone with Ctrl+Z; the same is true of the equivalent Undo command on Mac. A compile check confirms that the code is syntactically compilable, but it does not prove that the macro’s results are correct or safe.
Compile errors versus runtime errors
Label not defined is a compile error. VBA detects the unresolved reference before it can run the affected code. That is different from a runtime error, which occurs after execution begins—for example, when code attempts an operation that fails for the current data or environment. Adding an error handler cannot catch a compile error: the project must compile before that procedure can execute.
It is also different from a logic error. A procedure may compile successfully with a label that sends execution to the wrong section. In that case, VBA has found a valid destination, but the program may skip steps or produce an incorrect result. After fixing the compile error, test the normal route and any error route that matters, using a copy of important workbooks or data.
How to prevent the error returning
Keep labels descriptive and use them only when a jump makes the control flow easier to follow. For simple choices, If...Then...Else or Select Case is often clearer than multiple jumps. When you do use a label, update its references together if you rename it, and keep error-handler code near the end of its procedure so its role is easy to see.
Before sharing or relying on a workbook, compile the VBA project after substantial code edits. This catches unresolved references earlier than waiting for a user to run the affected macro. Also review the path into each label: confirm that required variables have been initialized and that normal execution cannot accidentally fall through into a handler. Compilation is a useful check, but it is not a substitute for testing the macro with representative inputs.
Frequently Asked Questions
Why does VBA say Label not defined when I can see the label?
Check that the label is in the same procedure as the jump and that its spelling matches. A label in another Sub, Function, or module is not a destination for that jump. Also check that the visible text is actually a label, with a colon after the name.
Can a GoTo jump to a label in another module?
No. A VBA GoTo target must be in the same procedure. To run code in another procedure, call that procedure by name where appropriate rather than trying to jump into its code.
Does On Error GoTo 0 cause this compile error?
Normally, no. On Error GoTo 0 is VBA’s special instruction for disabling the current procedure’s error handler, not a request to find a label named zero. A named target such as On Error GoTo ErrHandler does need a matching label.
Is Label not defined a runtime error?
No. It is a compile error, so VBA cannot run the affected code until the unresolved target is corrected. Runtime errors occur while code is executing.
Will adding the missing label always fix my macro?
It can fix the compilation problem, but only if that label is the correct destination. Review the code paths and test the macro afterward; a valid but inappropriate jump can still cause incorrect behavior.
Related
- Excel Tabs: How to Find Duplicate Names Across Sheets
- Excel SUM with Multiple Criteria: How to Do It
- Excel: Subtract the Same Value From Multiple Cells
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: How to Delete the Current RowTo delete the worksheet row containing the active cell, use VBA’s ActiveCell.EntireRow.Delete method. For a safer macro that…
- Send Email via Excel: Outlook, Mail Merge, VBA, and WorkflowsYes—you can send email using Excel, most often by combining a contact list with Word mail merge, using…
- Excel VBA Type Mismatch: Causes and FixesIn Excel VBA, “Run-time error 13: Type mismatch” means VBA tried to use a value as a different…
- Enum in VBA: What It Is and How to Use It with ExamplesIn VBA, Enum is a standard language keyword for declaring an enumeration: a named set of related constants,…
- Excel Macro to Import CSV Files AutomaticallyTo import a CSV file with an Excel macro, use VBA to ask you to select a file…
- Excel UserForm Controls: A Practical Guide to Types and UseExcel UserForm controls are the buttons, text boxes, lists, and other interface elements you add to a VBA…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.