logo
search
VBA & Macro Problems

How to Fix VBA Compile Error: Sub or Function Not Defined in Excel

John WilsonJohn Wilson Oct 21, 2026 868 views

Question details

The user needs to resolve a VBA compile error that occurs when a master macro attempts to execute several individual procedures.

How to Fix VBA Compile Error: Sub or Function Not Defined in Excel
Product
Excel
Device & OS
not provided
Scenario
Combining multiple functioning macros into a single button click to automate data pulling and application without pressing multiple buttons manually.
Observed behavior
A compile error reading 'Sub or Function not defined' is thrown, preventing the macro from running due to incorrect procedure names, missing worksheet references, or restricted access levels.
Before you start

Open the Visual Basic Editor (VBE) by pressing Alt + F11 and verify whether your individual macros are stored in a Worksheet module (e.g., Sheet3) or a Standard module.

Solution 1Recommended

Correct Module References and Access Levels

Ensure that macros located in worksheet modules are called using their specific worksheet code name and that all procedures have public access.

When a macro is stored in a specific worksheet module rather than a standard module, Excel requires you to specify the worksheet's code name when calling it from elsewhere. Additionally, macros marked as 'Private' cannot be accessed outside of their own module.

1
Verify access levels

Look at the first line of each macro you are trying to call. Ensure they begin with 'Public Sub' or simply 'Sub'. If they start with 'Private Sub', change it to 'Public Sub' so they can be triggered by your main button.

2
Reference worksheet modules

If the macros are located inside a specific worksheet object (like Sheet3), you must prefix the procedure name with the sheet code name when calling it. For example, write 'Sheet3.CopySystemCondition'.

3
Call standard module macros

If you move the macros into a Standard Module (e.g., Module1), you do not need the worksheet prefix. You can simply write the procedure name directly, such as 'CopySystemCondition'.

Correct Module References and Access Levels
Verify Exact Naming: Double-check that the spelling of your macro names in the call list exactly matches the actual procedure names to avoid lingering errors.
Powerful Macro Support

Edit and Run VBA Macros in WPS Office

WPS Spreadsheet offers comprehensive support for VBA and macros, allowing you to automate tasks and troubleshoot VBA code with an intuitive built-in editor.

  1. 1. Open your macro-enabled file: Launch WPS Spreadsheet and open your existing .xlsm or .xlsb workbook.
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab located on the top ribbon menu.
  3. 3. Open the VBA Editor: Click on the 'Visual Basic' button to view your modules, correct your macro references, and run the code.
  4. 4. Save your work: Once your 'Sub or Function not defined' error is resolved, click 'Save' to preserve the updated macro paths.
Fully compatible with Microsoft Excel .xlsm and .xlsb formatsBuilt-in Visual Basic Editor for seamless macro troubleshootingLightweight application with high performance for heavy data tasks
microsoft office alternative - wps office

Frequently Asked Questions

What does 'Sub or Function not defined' mean in VBA?

This error means the Visual Basic compiler cannot locate the macro or procedure you are trying to execute. It typically occurs if the procedure name is misspelled, marked as Private in a different module, or lacks the required worksheet module reference.

What is the difference between a Worksheet module and a Standard module?

A Worksheet module is attached directly to a specific sheet (like Sheet1 or Sheet2) and is best used for sheet-specific event handlers. A Standard module is independent of any specific sheet and is the recommended place for general macros that you want to call from anywhere in the workbook.

Do I need to use the 'Call' keyword to run another macro?

No, the 'Call' keyword is optional in VBA. You can simply write the name of the procedure to execute it. However, using 'Call' explicitly states your intention to run another macro, which can make your code easier for others to read and understand.