求助:声明Excel对象时出现“Compile Error: User-defined type not defined”错误
Hey there, let's work through this compile error you're hitting even after referencing the Microsoft Office Object Library 14.0. Here are the most likely fixes to try:
Double-check the Excel-specific library reference
The Microsoft Office Object Library covers general Office functionality, but Excel-specific objects (likeWorkbook,Worksheet, orApplication) require the Microsoft Excel Object Library (version 14.0 for Excel 2010). Open the VBA Editor, go to Tools > References, find this library in the list, and make sure it's checked.Verify your object declaration spelling
Typos are easy to miss! If you declared something likeDim xlApp As Excell.Application(note the extra 'l' in Excell) or misspelled a custom type name, that'll trigger this error. Double-check every object type in your declarations.Switch to late binding (no references needed)
If reference issues persist, late binding bypasses the need for library references entirely. Replace your early-binding code:Dim xlApp As Excel.Application Set xlApp = New Excel.ApplicationWith this late-binding alternative:
Dim xlApp As Object Set xlApp = CreateObject("Excel.Application")This also makes your code more compatible across different Excel versions.
Reset corrupted references
Sometimes library references get corrupted after Office updates or file moves. Try this:- Go to Tools > References in the VBA Editor.
- Uncheck all Office/Excel-related references.
- Click OK, then close and reopen the VBA Editor.
- Go back to References and recheck the necessary libraries (Office 14.0 and Excel 14.0).
Check for custom type name conflicts
If you've defined your own custom types (usingType...End Type), make sure none of their names clash with Excel's built-in object types (e.g., naming a custom typeWorkbook). Rename any conflicting custom types to avoid confusion.
Start with the Excel library reference check—it's the most frequent solution for this exact scenario. If that doesn't resolve it, late binding is a solid fallback that eliminates version-specific reference headaches.
内容的提问来源于stack exchange,提问作者Conrad Addo

