Excel VBA Sub or Function Not Defined? Fix It

VBA & macros · Updated November 19, 2026

“Sub or Function not defined” is a VBA compile error: Excel cannot resolve the name of a procedure or function used by your code. Check the highlighted name for a typo first, then confirm the procedure exists, is visible from the calling code, and belongs to a project your workbook can access.

What the error means

VBA compiles code before it runs it. When it reaches a call such as UpdateReport, it tries to find a Sub or Function with that name in the current VBA project or in a referenced project. If it cannot resolve the name, compilation stops and the editor highlights the call or, sometimes, the unresolved name within an expression.

This is different from a run-time error. A run-time error occurs after execution begins, perhaps because a worksheet is missing or a value is invalid. “Sub or Function not defined” generally means VBA cannot get past compilation, so the intended procedure has not started. The message does not necessarily mean the procedure is absent: it may exist under a different spelling, be private to another module, or be outside the project’s accessible scope.

To inspect the error, open the Visual Basic Editor with Alt+F11 on Windows. On Mac, Option+F11 is the usual shortcut, though the keyboard’s Fn setting can affect function-key behavior. Choose Debug > Compile VBAProject in the editor to check the project and move to a compile problem. The exact project name appears in the menu command.

Check the spelling and procedure name

Start with the word VBA highlights. Compare every character with the actual declaration, including underscores and spelling. VBA is not case-sensitive, so capitalization alone is not the cause, but a missing letter or different word is. For example, a call to UpdateReprot will not find a procedure named UpdateReport.

Also check that the procedure is declared as a Sub or Function rather than being only a variable, worksheet name, or comment. A common mistake is renaming a procedure in one place but not updating its callers. Use the editor’s Find command to search the current project for the name, and inspect the declaration line rather than relying on a call that looks similar.

If the name is ambiguous or a module has an unclear name, qualify a public procedure with its standard module name. For example, if a standard module is named modReport, the caller can use modReport.RefreshReport. Avoid giving a module the same name as one of its procedures; a descriptive module name such as modReport makes calls easier to read and troubleshoot.

Confirm the procedure is in an accessible module

Where code is stored affects how it can be called. Standard modules are the usual home for general-purpose public Subs and Functions. In the Project Explorer, choose Insert > Module to add one. A procedure declared Public in a standard module is generally callable elsewhere in the same VBA project, subject to its arguments and any naming conflicts.

A procedure declared Private can be called only from within its own module. If another module needs it, change the declaration to Public where appropriate, or move the call into the same module. For example, Private Sub RefreshReport() is deliberately hidden from callers outside that module. Changing it to Public is a visibility change, so do so only when broader access is intended.

Worksheet modules, the ThisWorkbook module, and class modules are object modules, not ordinary standard modules. Their procedures may depend on an instance of that object. A worksheet event procedure such as Worksheet_Change is designed to run when its event occurs; it is not a general-purpose macro to call by typing its name from anywhere. Put reusable code in a standard module, then call it from the event procedure.

Class modules also require attention to object instances. A public method in a class is normally called through an instance of that class, not as if it were a global procedure. If the intended code is a simple workbook-wide helper, a standard module is usually the straightforward location.

Use a valid call for a Sub or Function

A Sub performs an action and does not return a value. A Function returns a value, which can be assigned or used in an expression. Calling the wrong name is still the most common issue, but also check that you are using the routine in a way that matches its role and arguments.

For example, a public procedure in a standard module can be declared and called like this:

' In standard module modReport
Public Sub RefreshReport()
    ' Put the report-update steps here.
End Sub

' In another module in the same project
Public Sub RunRefresh()
    ' Calls the public procedure in modReport.
    modReport.RefreshReport
End Sub

In VBA, the Call keyword is optional for a Sub. If you use it, put arguments in parentheses; without Call, a no-argument call can be written as above. A Function can be called in an assignment, as in result = AddTax(amount), or used as part of an expression. Check that the number and types of arguments also match the function declaration; argument problems may produce a different compile message, but correcting the name alone will not fix an incompatible call.

When you add or edit a macro, save a copy of the workbook before running it. Macros cannot be undone with Ctrl+Z, and a code change can affect workbook contents.

Call Excel worksheet functions correctly

Another source of confusion is assuming that every function available in a worksheet can be typed directly into VBA as if it were a VBA language function. Some functions are built into VBA, while many worksheet functions are accessed through Excel’s object model. For a worksheet function that Excel exposes through this interface, use Application.WorksheetFunction or, where suitable, Application.

For example, code can call the worksheet SUM function through the WorksheetFunction object. If VBA instead highlights a bare name such as Sum, it may be treating it as an unresolved VBA procedure rather than the worksheet function you intended. The WorksheetFunction form can raise a run-time error for certain worksheet-function errors; using Application directly can return a Variant that may contain an error value, so choose based on how the code should handle errors.

Not every Excel worksheet function is available in precisely the same way through WorksheetFunction, and new worksheet functions may depend on the Excel version. If a function is not exposed as expected, check Microsoft’s documentation for that function and the object model, or consider a different implementation. Do not assume that installing a newer Excel version automatically makes every worksheet function a directly callable VBA language function.

Check references and other workbooks

VBA projects can use external object libraries and other VBA projects through references. In the Visual Basic Editor, choose Tools > References and look for an entry beginning with “MISSING:” in the selected project. A broken reference can cause compilation problems, though the error may instead say “Can’t find project or library” or identify a missing type. This is related to, but not always identical to, “Sub or Function not defined.”

If a reference is missing, first determine which library the code actually needs. If it is not required, remove the broken reference and revise any code that relied on it. If it is required, restore the correct library or update the code to use a supported alternative. Do not uncheck references at random: doing so can break valid declarations elsewhere in the project.

Code in another open workbook is not automatically a global procedure in the current workbook. The calling project may need a reference, or the code may be invoked by an appropriate mechanism such as Application.Run with the workbook and macro name. The target workbook must be available, and names must match. If a macro is intended for repeated use across workbooks, an Excel add-in or a deliberately referenced shared project may be a better arrangement than copying calls between unrelated projects.

References and available libraries can differ by platform and installation. A project written on Windows may depend on a library that is unavailable on a particular Mac setup. If the code must run on both platforms, review its dependencies rather than assuming that a reference selected on one computer exists on another.

Use the editor to isolate the cause

A systematic check is faster than changing several declarations at once. Work through the following sequence:

  1. In the error dialog, choose Debug and note the exact highlighted identifier.
  2. Search the project for that identifier and find the actual Sub or Function declaration.
  3. Compare spelling, module location, Public or Private status, and required arguments.
  4. If the code calls a worksheet function, confirm that it is being invoked through a valid VBA interface.
  5. Open Tools > References and inspect any missing library entries.
  6. Run Debug > Compile VBAProject again after each correction, then test the relevant macro with a copy of the workbook.

If Compile is unavailable or does not reveal the issue, make a small code change and try again, since the editor may only compile when it detects a change. Compilation checks syntax and references; it does not prove that the macro’s logic is correct or that every run-time condition will succeed.

Common misconceptions and nearby errors

“The macro is missing from the Macro dialog” is not the same diagnosis. A procedure may be absent from that list because it is Private, takes arguments, or is stored in an object module, even though some other code can call it. Conversely, making a procedure Public may make it accessible but will not correct a misspelled call or a missing dependency.

Option Explicit is useful because it requires variables to be declared. A misspelled variable under that setting often produces “Variable not defined,” not “Sub or Function not defined.” Keep the distinction in mind: an unresolved name used like a procedure points toward a call or function; a name used like a variable points toward a declaration. The highlighted text and the full message help identify which case applies.

“Expected: variable or procedure, not module” is another distinct compile error. It can appear when a module name is used where VBA expects a procedure or when names collide. Renaming modules clearly and qualifying calls can help, but read the actual message rather than treating every compile error as the same one.

Finally, an Excel macro blocked by security settings is a separate issue. A disabled macro does not by itself explain a compile-time unresolved procedure. Once the project compiles, handle macro security through trusted, appropriately sourced files and your organization’s rules; do not enable unknown code simply to get past an error.

Frequently Asked Questions

Why does VBA say Sub or Function not defined when the code is there?
It may be Private, stored in a module that requires an object instance, spelled differently at the call site, or located in another VBA project that is not referenced or invoked correctly. Check the declaration and its scope, not just whether a similarly named line exists.

How do I fix Sub or Function not defined in Excel VBA?
Use Debug to identify the unresolved name, correct its spelling, and verify that the intended procedure is accessible from the caller. Then check function-call syntax, worksheet-function access, and Tools > References before compiling again.

Can a Private Sub be called from another module?
No. A Private procedure is limited to its own module. If another module should call it, use Public when appropriate or keep the caller within the same module. A procedure’s location in a worksheet or class module can also affect how it is accessed.

Why can’t I call an Excel formula directly in VBA?
Worksheet formulas and VBA language functions are not interchangeable. Many worksheet functions must be called through Excel’s object model, commonly with Application.WorksheetFunction or Application. Confirm that the particular function is exposed and supported in the Excel version being used.

Does a missing reference cause this exact error?
Sometimes a broken dependency contributes to compilation failures, but VBA may report a different message, such as “Can’t find project or library” or “User-defined type not defined.” Check Tools > References and diagnose the exact highlighted name and message.

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.