在没有编译/运行时错误的情况下,如何识别损坏的VBA代码? [英] How to identify broken vba code in absence of compile/run-time errors?

查看:174
本文介绍了在没有编译/运行时错误的情况下,如何识别损坏的VBA代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在没有编译/运行时错误的情况下是否有任何方法可以识别所有损坏的vba代码(例如:变量)?

I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?

例如:

假设在ModuleA.bas中有一个变量定义为:

Let's say there is a variable defined in ModuleA.bas as:

Public Const REG_SZ AS Long = 1

其他文件(例如ModuleB.bas,ModuleC.bas等)都使用此变量.

Other files, say ModuleB.bas, ModuleC.bas, etc make use of this variable.

现在,如果删除包含变量REG_SZ的ModuleA.bas文件并构建项目,则不会看到编译或运行时错误. 只有在调试时,您才知道程序默认将此变量设置为没有值.当您在其他模块中右键单击此变量,然后单击定义"时,将显示一条错误消息:无法识别光标下的标识符".解决方案是在每个文件中本地声明此变量,或者重新-将原始ModuleA.bas文件声明为已声明的变量.

Now, if we delete the ModuleA.bas file that contains the variable REG_SZ, and build the project, then we see no compile or run-time errors. Only while debugging you realize that the program defaults this variable to be having no value. When you right click this variable in the other modules and click on 'Definiton' you get an error that says: "Identifier under cursor is not recognized". The solution is to declare this variable locally on each file, or re-instate the original ModuleA.bas file with that variable declared.

我想知道在没有编译/运行时错误的情况下是否有任何方法可以识别所有损坏的vba代码(例如:变量)?

I would like to know if there is any way to identify all the broken vba code (example: variables) in absence of compile/run-time errors?

推荐答案

要回答有关变量的特定问题...在每个代码模块的顶部放置Option Explicit语句.您插入的任何未声明其变量的VBA代码都将被自动标记,而无需编译该代码.

To answer your specific question on variables ... Put an Option Explicit statement at the top of each code module. Any VBA code you insert that does not declare its variables will automatically be flagged without having to compile the the code.

这篇关于在没有编译/运行时错误的情况下,如何识别损坏的VBA代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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