Excel VBA编译引发“未定义的用户定义类型”错误,但不会成为违规代码行 [英] Excel VBA Compile throws a "User-defined type not defined" error but does not goto offending line of code

查看:452
本文介绍了Excel VBA编译引发“未定义的用户定义类型”错误,但不会成为违规代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是编译一个 Excel VBA项目时的一个症状。发生以下错误:

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

未定义的用户定义类型

但是,编译器没有突出显示该错误的代码,所以我无法确定问题。

However, the code that produces this error is not highlighted by the compiler and so I cannot identify the issue.

这是一个用户定义的类型未定义错误,我以前看到过简单的问题,例如命名某些东西 As Strig 而不是 As String 。不过这个特殊的错误只是在$ code> Debug>编译VBAProject 菜单选项,当弹出错误消息框时,它不会突出显示错误发生的代码行。

This is a "User-defined type not defined" error that I have seen before with simple issues such as naming something As Strig instead of As String. However this particular error is only popping up during the Debug > Compile VBAProject menu option and when the error message box pops up it does not highlight the line of code that the error is occuring in.

之后很多研究我发现这个bug可能与缺少的引用相关,我已经排除了这一点,因为我已经包括了所有需要的引用和Toolbox对象。

After a lot of research I have found that this bug can be related to missing references and I have ruled this out as I have included all needed references and Toolbox objects.

确保我没有丢失任何明显缺失的 Dim 语句我已经向所有代码页(包括表单)添加了 Option Explicit 确保没有任何东西丢失。运行编译时,错误仍然显示。

To ensure I wasn't missing any obvious missing Dim statements I have added Option Explicit to all code pages (forms included) to make sure nothing was missing. The error still shows when running a compile.

还有这个已知的错误说明由于使用二进制兼容性的 VB6 项目已经知道这个问题:

There is also this known bug that states the issue has been known to happen because of the VB6 projects using binary compatibility:


关闭二进制兼容性并编译项目。 Visual Basic
将突出显示未定义的用户定义类型
的代码行。解决问题后,二进制兼容性
可以重新启动。

Turn off Binary Compatibility and compile the project. Visual Basic will highlight the line of code that contains the User Defined Type that is not defined. After resolving the problem, Binary Compatibility can be turned back on.

我通过这个问题和答案,但是,我找不到此选项标准的Excel VBA 编辑器。

I found this article via this Question and Answer, however, I cannot find this option in the standard Excel VBA editor.

我从Google搜索和其他问题中知道,我不是唯一一个遇到这个问题的人。

I know from Google searches and other questions that I am not the only one who has had this issue.

我已经尝试手动执行代码,但也只是许多行可行地执行。

I have tried going through the code manually but there are simply too many lines to feasibly do so.

有没有办法关闭Excel VBA项目中的二进制兼容性?如果他们无法调试他们需要改变的内容,人们如何找到这个冒犯行的代码?任何帮助将是可爱的!

Is there a way of turning off Binary Compatibility in Excel VBA projects? How do people find this offending line of code if they can't debug to what they need to change? Any help would be lovely!

提前谢谢。

编辑: 我发现了令人讨厌的代码行,所以我的特定问题解决了在删除这个特定的行后,问题仍然在这里 - 这是代码中引用的表单上的一个错误的控件名称。这还没有解决特定的问题,你会如何找到这个违规的代码是问题。当这个错误发生时,我们能找到一个找到违规代码的好方法,以便将来可以避免这种痛苦吗?

I have found the offending line of code and so my particular issue is solved The problem is still here after removing that particular line - it was a mispelt control name on a form being referenced in it's code. This still does not solve the particular issue of how you would go about finding this offending code was the issue. Are we able to find a good way of finding the offending code when this bug happens so others in the future can avoid this agony?

推荐答案

p>因为听起来你已经尝试了许多不同的电位解决方案,所以现在可能需要这么长时间的方法。

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.

要么你会发现罪犯,要么你会有一个新的工作簿,神奇地没有问题。后者将由于工作簿文件中的某些隐藏的损坏,可能在二进制vbproject部分。

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!

这篇关于Excel VBA编译引发“未定义的用户定义类型”错误,但不会成为违规代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆