The "User-defined type not defined" error in Excel VBA occurs when the code references an object, class, or data type that the VBA editor does not recognize. This happens for one of two reasons: you are using a library (like Outlook or FileSystemObject) without enabling the required reference in the Tools menu, or you have declared a custom Type (UDT) in a location where the current procedure cannot see it. To fix this, you must either check the missing library in the References list or move your Type declaration to the top of a public module.

What You'll Need

  • Microsoft Excel installed on a Windows PC.
  • A workbook containing the VBA code that is triggering the error.
  • Administrative permissions to access the VBA Editor (VBE) and modify references.

Step-by-Step

  1. Identify the Culled Variable: When the error occurs, VBA will highlight a specific word in yellow or blue. Note the name of the variable causing the crash. For example, if the line Dim fso As FileSystemObject is highlighted, the missing type is "FileSystemObject".
  2. Open the VBA Editor: Press Alt + F11 on your keyboard to open the Visual Basic for Applications editor.
  3. Access the References Menu: In the top menu bar, click on Tools and then select References.... (Note: You must stop the code from running—click the square Reset button in the toolbar—before the References menu becomes clickable).
  4. Locate the Missing Library: Scroll through the list of available libraries. Depending on the variable you identified in Step 1, look for the corresponding library. Common mappings include:
    • For FileSystemObject: Look for Microsoft Scripting Runtime.
    • For Outlook objects: Look for Microsoft Outlook XX.0 Object Library.
    • For Word objects: Look for Microsoft Word XX.0 Object Library.
    • For Regular Expressions: Look for Microsoft VBScript Regular Expressions 5.5.
  5. Enable the Reference: Check the box next to the required library and click OK.
  6. Verify Custom Type Declarations: If you are using a custom Type (e.g., Type EmployeeInfo) and the reference check didn't work, check where the Type is defined. If it is inside a specific Sub or Function, it is local and cannot be used elsewhere. Move the Type...End Type block to the very top of a standard Module, above all procedures, and ensure it is preceded by the keyword Public.
  7. Compile the Project: To ensure the error is gone, go to the Debug menu and select Compile VBAProject. If no errors appear, the issue is resolved.

Common Mistakes

One of the most frequent mistakes is attempting to use Early Binding without setting the reference. Early Binding (using Dim x As SpecificType) provides IntelliSense and faster execution but requires the reference to be checked. Many users copy code from the internet that assumes the reference is already enabled, leading to immediate failure upon execution.

Another common error is placing a Public User-Defined Type (UDT) inside a Class Module or a Sheet Module. In VBA, Public UDTs must be declared in a standard Module (Insert > Module). If you place a Public Type in a Sheet module, other modules cannot see it, resulting in the "User-defined type not defined" error even though the code looks correct.

Users also frequently forget to stop the code before trying to access the Tools menu. If the code is in "Break Mode" (paused on an error), the References option is greyed out. You must click the Reset button (the blue square) to return to design mode before you can modify the library references.

Tips

If you intend to share your Excel workbook with other people who may have different versions of Office installed, avoid Early Binding. Instead, use Late Binding. Late Binding does not require you to check any boxes in the References menu because it resolves the object type at runtime.

To convert Early Binding to Late Binding, change your declaration from Dim fso As FileSystemObject to Dim fso As Object. Then, instead of using Set fso = New FileSystemObject, use Set fso = CreateObject("Scripting.FileSystemObject"). This makes your code much more portable and prevents the "User-defined type not defined" error from appearing on other users' computers.

Always use the Compile VBAProject feature under the Debug menu after making changes. This allows you to find all instances of undefined types across your entire project at once, rather than discovering them one by one as the code runs.

Troubleshooting

If you have checked the correct reference but the error persists, check for "MISSING:" entries in the References list. If you see a library labeled MISSING: Microsoft [Library Name], it means the workbook was created on a computer with a different version of Office. Uncheck the missing library, find the version that matches your current installation in the list, check it, and click OK.

If you are using a custom Type and still getting the error, ensure you haven't accidentally named your Type the same as a reserved VBA keyword or an existing library object. For example, naming a custom type Range or Workbook will cause significant conflicts and may trigger undefined type errors.

In cases where the VBA editor seems to "forget" the reference after saving, check if your file is saved as an .xlsx file. .xlsx files cannot store macros or VBA projects. Ensure your file is saved as an .xlsm (Macro-Enabled Workbook) or .xlsb (Binary Workbook) to preserve the project settings and references.

Frequently Asked Questions

Why does this happen when I move my file to a different computer?

This usually happens because the original author used a specific version of an object library (e.g., Outlook 16.0) and your computer has a different version (e.g., Outlook 15.0). VBA cannot automatically map these versions, leading to a missing reference.

What is the difference between a Class and a User-Defined Type?

A User-Defined Type (UDT) is a simple structure used to group different variables of different types together. A Class is a more powerful object that can contain both data (properties) and logic (methods/procedures). UDTs are declared with Type, while Classes require a separate Class Module.

Can I fix this without using the References menu?

Yes, by using Late Binding. By declaring your variables As Object and using CreateObject(), you bypass the need for the References menu entirely, as VBA looks up the object library while the code is actually running.

Does this error occur with standard types like Integer or String?

No. Standard data types (Integer, String, Long, Double, Boolean) are built into the VBA language. This error only occurs for complex objects from external libraries or custom types you have created yourself.

Is there a way to automatically check for missing references?

VBA does not have a built-in "auto-fix" for references. However, you can write a script to check the VBProject.References collection, though this requires enabling "Trust access to the VBA project object model" in the Excel Trust Center settings.