检查 Excel VBA 中公共变量的值 - Locals Window Alternative
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15031323/
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
Checking the values of public variables in Excel VBA - Locals Window Alternative
提问by psychonomics
I have been using the Locals window to check the assignments for procedure level variables.
我一直在使用 Locals 窗口来检查过程级变量的分配。
I have recently updated my code to create a set of public-level variables that read certain inputs from the sheets which do not change from project to project.
我最近更新了我的代码以创建一组公共级变量,这些变量从工作表中读取某些输入,这些输入不会因项目而异。
When seeking to check these variables, I do not see them in the Locals window, no doubt because they are not locally-defined variables!
当试图检查这些变量时,我在 Locals 窗口中没有看到它们,这无疑是因为它们不是本地定义的变量!
Is there an alternative to the Locals Window for Public variables? And if not, how am I supposed to check public variable assignments?
公共变量的局部窗口是否有替代方法?如果没有,我应该如何检查公共变量分配?
回答by Peter Albert
In addition to the Immediate window (as described in the other answer), the Watch window is very helpful in those circumstances. You can activate it in the Viewmenu -> Watch window:
除了立即窗口(如其他答案中所述)之外,监视窗口在这些情况下也非常有用。您可以在View菜单 -> Watch window 中激活它:
Here you can define:
您可以在这里定义:
- Any variable (e.g. your public variables)
- A full term, e.g.
ActiveWorkbook.UsedRange.Address
- The scope of each watch
- Even a breakpoint when the value changes or results in
True
- this is quite handy when debugging, as it allows you quick conditional breakpoints without adding additional code, e.g. if you set a watch tomyVar=0
and active Break When Value Is True, the code will automatically stop the moment the potential bug is "initiated"
- 任何变量(例如您的公共变量)
- 一个完整的术语,例如
ActiveWorkbook.UsedRange.Address
- 每只手表的范围
- 即使是值更改或结果时的断点
True
- 这在调试时非常方便,因为它允许您在不添加额外代码的情况下快速条件断点,例如,如果您将监视设置为myVar=0
并激活Break When Value Is True,代码将自动停止“启动”潜在错误的那一刻
You can add all of these options in the "Add watch" dialog, which you either get by right clicking on any variable (or other code) in the code module - or by right clicking in the watch window:
您可以在“添加监视”对话框中添加所有这些选项,您可以通过右键单击代码模块中的任何变量(或其他代码)或右键单击监视窗口来获得这些选项:
In addition, you can simply edit any watch items in the list by double clicking.
此外,您可以通过双击简单地编辑列表中的任何监视项目。
Very handy tool for debugging, esp. in combination with Localsand Immediatewindows.
非常方便的调试工具,尤其是。结合Locals和Immediate窗口。
回答by Peter Albert
Use Immediate Window. Press Ctrl + G in your coding envtheitroadnment to goto Immediate Window. It allows to get and set the value of your variable when you are in debugging mode. You need to prefix ? to check the value of variable. example
使用立即窗口。在您的编码环境中按 Ctrl + G 以转到立即窗口。当您处于调试模式时,它允许获取和设置变量的值。你需要前缀吗?检查变量的值。例子
?variableName press enter to get the value
variableName ="test" press enter to set the value