Compile throws a “User-defined type not defined” error but does not go to the offending line of code

前端 未结 21 1265
再見小時候
再見小時候 2020-12-14 08:00

Symptoms

This is a symptom specifically when compiling an Excel VBA project. The following error occurs:

User-defined type not defin

相关标签:
21条回答
  • 2020-12-14 08:24

    Since it sounds like you've tried many different potentional solutions, you'll probably have to do this the long methodical way now.

    Create a new blank workbook. Then piece by piece copy your old workbook into it. Add a reference, write a little bit of code to test it. Ensure it compiles, ensure it runs. Add a sub or function, again, write a little test sub to run it, also ensure it compiles. Repeat this process slowly adding and testing everything.

    You can speed this up a bit by first trying larger chunks, then when you find one that triggers the problem, remove it and break it into smaller peices for testing.

    Either you will find the offender, or you'll have a new workbook that magically does not have the problem. The latter would be due to some sort of hidden corruption in the workbook file, probably in the binary vbproject part.

    Welcome to the world of debugging without debuggers or other helpful tools to do the heavy lifting for you!

    0 讨论(0)
  • 2020-12-14 08:26

    I had exactly the same problem (always seems to occur when I try to implement a Interface onto a userform. Download and install Code Cleaner from here. This is a freeware utility that has saved me on numerous occasions. With your VBA project open, run the "Clean Code..." option. Make sure you check the "backup project" and/or "export all code modules" to safe locations before running the clean. As far as I understand it, this utility exports and then re-imports all modules and classes, which eliminates compiler errors that have crept into the code. Worked like a charm for me! Good luck.

    0 讨论(0)
  • 2020-12-14 08:26

    An different problem with the same symptom: I had a class implemented which was not defined. It was wrapped with a #if that I thought should have not allowed the compiler to see it, but not so. Remove the comment out the Implements statement and all is well. I assume importing the definition would also work...

    0 讨论(0)
  • 2020-12-14 08:28

    For the Scripting.Dictionary type, you can either use late binding (as already pointed out ) with:

    Dim Dict as Object
    Set Dict = CreateObject("Scripting.Dictionary")
    

    Which works, but you don't get the code auto completion. Or you use early binding, but you need to make sure that VBA can find the Scripting.Dictionary type by adding the reference to the Microsoft Scripting Library via VBA-->Tools-->References--> "Microsoft Scripting Runtime". Then you can use:

    Dim Dict as Scripting.Dictionary
    Set Dict = New Scripting.Dictionary
    

    ... and auto completion will work.

    0 讨论(0)
  • 2020-12-14 08:28

    I had the same issue, if you enable Microsoft Scripting Runtime you should be good. You can do so under tools > references > and then check the box for Microsoft Scripting Runtime. This should solve your issue.

    0 讨论(0)
  • 2020-12-14 08:29

    I was able to fix the error by

    1. Completely closing Access
    2. Renaming the database file
    3. Opening the renamed database file in Access.
    4. Accepted various security warnings and prompts.
      • Not only did I choose to Enable Macros, but also accepted to make the renamed database a Trusted Document.
      • The previous file had also been marked as a Trusted Document.
    5. Successfully compile the VBA project without error, no changes to code.
    6. After the successful compile, I was able to close Access again, rename it back to the original filename. I had to reply to the same security prompts, but once I opened the VBA project it still compiled without error.

    A little history of this case and observations:

    • I'm posting this answer because my observed symptoms were a little different than others and/or my solution seems unique.
    • At least during part of the time I experienced the error, my VBA window was showing two extra, "mysterious" projects. Regrettably I did not record the names before I resolved the error. One was something like ACXTOOLS. The modules inside could not be opened.
    • I think the original problem was indeed due to bad code since I had made major changes to a form before attempting to update its module code. But even after fixing the code the error persisted. I knew the code worked, because the form would load and no errors. As the original post states, the “User-defined type not defined” error would appear but it would not go to any offending line of code.
    • Prior to finding this error, I ensured all necessary references were added. I compacted and repaired the database more than once. I closed down Access and reopened the file numerous times between various fix attempts. I removed the suspected offending form, but still got the error. I tried other various steps suggested here and on other forums, but nothing fix the problem.
    • I stumbled upon this fix when I made a backup copy for attempting drastic measures, like deleting one form/module at a time. But upon opening the backup copy, the problem did not reoccur.
    0 讨论(0)
提交回复
热议问题