VBA 中有没有办法了解未使用的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way in VBA to know about variables that aren't being used?
提问by Lance Roberts
Is there a tool, method or setting in the standard VBA Editor to warn about variables that have been Dim
'med, but aren't being used?
标准 VBA 编辑器中是否有工具、方法或设置来警告已被Dim
“处理”但未被使用的变量?
回答by Jon Fournier
MZ-Toolswill search through your code and tell you what is not being used. The version for VBA can be found here.
MZ-Tools将搜索您的代码并告诉您哪些未被使用。VBA 的版本可以在这里找到。
The specific feature in MZ-Tools that performs what you asking about is Review Source Code:
MZ-Tools 中执行您所询问的特定功能是Review Source Code:
The Review Source Code feature allows you to review the source code to detect some unused declarations (constants, variables, parameters, procedures, etc.).
查看源代码功能允许您查看源代码以检测一些未使用的声明(常量、变量、参数、过程等)。
回答by Greedo
Another method (because I don't have admin privileges to install that software), is to put Option Explicit
at the top of your module. Then comment out all the Dim
'med variables, and debug through your code with F8or recompile with Alt+d+l
另一种方法(因为我没有安装该软件的管理员权限)是放在Option Explicit
模块的顶部。然后注释掉所有Dim
通过您的代码以“配有变量,调试F8或重新编译 Alt+ d+l
Uncomment the Dim
s one by one until Option Explicit
returns no compiler errors, at which point any variables still commented out can be ignored, as they are not used in your code
将Dim
s 一一取消注释,直到不Option Explicit
返回编译器错误,此时任何仍被注释掉的变量都可以忽略,因为它们未在您的代码中使用
Similar to MZ-Tools, RubberDuckhas a VariableNotUsed Inspection(amongst many other things) that should show precisely what you're after. It's free and, as of v2.2.0.3086, it supports non-admin installation.
与 MZ-Tools 类似,RubberDuck有一个VariableNotUsed 检查(在许多其他方面),它应该准确地显示您所追求的内容。它是免费的,从 v2.2.0.3086 开始,它支持非管理员安装。