This error occurs when a VBA procedure tries to call a procedure or function that has not been declared or defined in the current scope, or when it attempts to access a function by a name that does not exist in the referenced library. The most common cause is a typo in the function name or a missing reference to a library that contains the function. You can fix this by checking the spelling of the function name in your code and ensuring the correct library is referenced under the Developer tab.
What You'll Need
- A computer running Windows with Microsoft Excel, Access, Word, or another Office application installed.
- The VBA Editor (IDE) open in the application where the code resides.
- A basic understanding of how to use the References dialog in the IDE.
- A text editor or the immediate window to test and verify code snippets.
Step-by-Step
- Open the VBA Editor. Press Alt + F11 on your keyboard to launch the Visual Basic for Applications editor. If you are in Excel, you can also find this by navigating to the Developer tab on the ribbon and clicking Visual Basic. If the Developer tab is not visible, go to File > Options > Customize Ribbon and check the box for Developer under the Main Tabs list.
- Locate the offending code. In the Project Explorer window (usually on the left side of the editor), double-click on the Module, Form, or Class that contains the code causing the error. This window lists all the components of your project. Look for the line of code flagged with a yellow arrow or highlighted in yellow, which indicates the line that triggered the Run-time error '452': Invalid ordinal.
- Inspect the function call. Look at the specific line of code causing the crash. It will look something like
result = SomeFunction(argument1)orCall AnotherProcedure. The error suggests that the computer cannot find a procedure namedSomeFunctionorAnotherProcedureat this point in the code execution. - Check for typos. Compare the name used in the call with the name used in the declaration. Even a single character difference will cause this error. For example, if the function is defined as
Public Sub CalculateTax(), but you are calling it asCalculateTaxes, the system will raise error 452 because it is looking for a function that does not exist. Correct the spelling in the calling line to match the declaration exactly. - Verify the scope of the function. Ensure that the function you are trying to call is visible to the current procedure. If the function is defined as
Privateinside a different module, it cannot be called from the current module. You must either change the declaration toPublicor move the function definition to the current module. - Check for duplicate names. If you have two procedures with the same name in the same module, or if you have a procedure with the same name as a built-in VBA function (like
MsgBoxorLen), VBA will use the local one or the built-in one depending on context. However, if you are trying to call a specific library function but have shadowed it with a local variable or function, you may get this error. Rename your local procedures to avoid conflict with standard library names. - Open the References dialog. If the function name appears correct and the scope is right, the issue is likely that the function resides in an external library. Press Ctrl + G to open the Immediate Window, or go to the Tools menu at the top of the editor and select References. This lists all the DLLs and type libraries that VBA can access.
- Search for the missing reference. Scroll through the list of checked references in the dialog. Look for any entries that are marked as MISSING. These indicate that a library file is missing from your system or has been moved, but the code is still trying to use it. Common missing references include specific Windows API libraries or third-party add-ins.
- Resolve the missing reference. If you find a MISSING reference, uncheck it and then try to find the correct file in the Browse button. You will need to navigate to the folder where the .dll or .tlb file is located. For standard Windows libraries, this is often in the System32 or SysWOW64 folders, but for custom libraries, it depends on where they were installed.
- Recompile the project. After checking or correcting references, go back to the Debug menu and select Compile [ProjectName]. This forces the IDE to re-parse all the code and resolve every function call. If the error persists after compiling, the problem is likely a typo or a scope issue that was missed in the previous steps.
Common Mistakes
One of the most frequent mistakes is assuming that a function name is correct simply because it is highlighted in blue (indicating it is recognized by the editor) or because it is part of a VBA library. Error 452 specifically indicates that while the syntax might be valid, the ordinal (the specific entry point or index) is invalid. This often happens when referencing API functions in a 32-bit environment from a 64-bit library, or when a library upgrade has changed the internal index of the function without changing its name.
Another common error is failing to distinguish between a User-Defined Function and a standard library function. If you define a Sub or Function with the same name as a standard VBA function (like Round or Len), you are creating a shadow. If you then try to call the standard function from within that same scope, VBA might not find it as expected, leading to confusion when the error is reported.
Tips
Use the Auto List Members feature to avoid typos. When you type FunctionName( in the editor, a list of available functions and their arguments should pop up. If no list appears, it usually means the function is not recognized or not in scope, which is a strong indicator of the error. You can enable this feature by going to Tools > Options > Editor and checking Auto List Members.
When working with external libraries, pay attention to the architecture (32-bit vs 64-bit). If you are running a 64-bit version of Windows and Office, but try to load a 32-bit library, or vice versa, the ordinal mapping can be incorrect, resulting in this runtime error. Ensure that all references point to compatible versions of the libraries.
Troubleshooting
If you have checked the spelling, verified the scope, ensured the correct references are selected, and still receive the error, you may need to clean up your project. Go to the File menu and select Remove Module or Remove Form. When prompted to save changes, click No. This deletes the component from the project file. Then, create a new, blank module and copy your code into it one section at a time. Run the code after moving each section to see where the error occurs within the new module, as this can sometimes resolve corruption in the project file.
If the error happens only when the code is run on a different computer, the issue is definitely a missing reference. Compare the References dialog on the working machine with the one on the broken machine. You may need to manually install the missing library on the target machine or instruct the user to load the references.
For API declarations, ensure that the PtrSafe keyword is used correctly for 64-bit systems. If this keyword is missing on a 64-bit system, or present on a 32-bit system, it can cause the binding to fail, resulting in an invalid ordinal error when the code attempts to execute the call.
Frequently Asked Questions
Why does VBA say 'Invalid ordinal' when I'm sure I spelled the function name right? This error usually means the function name is correct, but the location or library containing it is not accessible. It often points to a missing reference to a specific DLL or type library where that function resides, or a conflict in the 32-bit versus 64-bit architecture of the application and the library.
Does error 452 mean my code is broken? Not necessarily. It is a specific error that tells you the code is trying to call a procedure that exists in a library, but the system cannot locate the specific entry point for it. It is a communication error between your code and the external library, rather than a syntax error in your writing.
Can I fix this by reinstalling Microsoft Office? Reinstalling Office can fix missing system libraries, but it is a drastic measure. The first step is to check the References dialog in the VBA editor to see if a library is marked as MISSING. If one is found, you may only need to point to the correct file or install the missing software component.
Is this error common with Windows API calls? Yes, it is very common when calling Windows API functions. It typically happens if the API declaration in your module does not match the actual function layout in the system DLL, or if the project is compiled for one architecture (32-bit) but the DLL intended for use is for the other (64-bit).
How do I know if a reference is missing? In the References dialog, look for any entries that have a checkmark but are not fully expanded, or entries that are explicitly labeled as MISSING. These are the culprits for this error. A missing reference prevents VBA from finding the underlying function implementation.