调试 VBA 代码
本页介绍了调试 VBA 代码的方法。
介绍
调试程序是软件开发中最重要的步骤之一。了解 VBA 的调试工具可以使调试变得更容易、更高效。本页介绍了在测试和调试应用程序时可以使用的几个 VBA 内置调试工具。
单步执行代码
One of the first methods to debug code is to step through the code one line at a time. To step through code, put the cursor on the first line of code to be analyzed and press F8 or choose Step Into on the Debug menu. The next line of code to be executed will be displayed in yellow background with a black font. Note that the highlighted line of code has not yet been executed -- it is the next line to execute.
If your code calls another procedure, stepping through the code with F8 will cause execution to enter the called procedure in a line-by-line sequence. If you want to execute the called procedure without stepping through it, press Shift+F8. This will execute the called procedure and then pause on the line of code after calling the procedure. If you are already stepping through a procedure, you can press Ctrl+ F8 to resume code execution line-by-line. At any time you are paused either in step-by-step mode or at a breakpoint (see below), you can press F5 or Continue from the Run menu to cause VBA to run to completion or until a pause statement is encountered.
每当您在逐步模式下暂停时,您都可以从即时窗口.
断点和停止命令
A breakpoint is a marker placed on a line of code that causes execution to pause immediately before executing that line. You can add a breakpoint to a line of code by putting the cursor on the line of code in question and pressing F9, choosing Toggle Breakpoint on the Debug menu, or clicking in the left margin next to the line of code. When a breakpoint is set, the line is displayed in brick-red background with a white font. When you run the code, execution will pause immediately before the line of code with the breakpoint and will display it in yellow background with a black font. Note than the line in yellow has not yet been executed -- it is the next line of code to run.
While the code is paused at the breakpoint, you can issue commands in the Immediate window to change or query a variable's value. To view the content of a variable, enter a ? character followed by the name of the variable and then press Enter. You can change a variable's value by entering VariableName = NewValue
in the Immediate window and pressing Enter.
If the Immediate window is not visible (typically at the bottom of the VBA Editor screen), press Ctrl+G or choose Immediate Window from the View menu to make the window visible.
To remove a breakpoint, put the cursor on the line of code and press F9. You can clear all breakpoints by choosing Clear All Breakpoints from the Debug menu or pressing Ctrl+Shift+F9. VBA also provides the Stop
command. This simply stops code execution on that line of code and enters break mode.
Once you are finished debugging the code, be sure to go back and clear all breakpoints (choose Clear All Breakpoints from the Debug menu or press Ctrl+Shift+F9) and be sure to remove or comment out all Stop statements.
当您在断点处或在单步模式下暂停时,您可以更改要执行的下一行,可以在当前行之前重新运行一段代码,也可以在该行之后跳过语句。右键单击要恢复执行的行,然后右键单击并选择设置下一条语句或选择设置下一条语句来自Run菜单。执行将在选定的代码行处恢复。
调试命令
VBA 提供了一个具有两个属性的 Debug 对象:Print 和 Assert,您可以使用它们显示变量的值并控制程序流程。Debug.Print
会将其后面的内容写入立即窗口。代码执行不会被中断。在立即窗口中显示文本后,代码执行继续运行。您可以将文字文本与变量名称混合在一起Debug.Print
陈述。例如,
Debug.Print "The value of variable X is: " & X
您可以在“立即”窗口中同时显示多个变量,并用逗号分隔它们。例如,
Debug.Print X, Y, Z
The Debug.Assert
command 是一个条件断点,如果 Assert 语句后面的表达式为 False,则该断点将导致执行在 Debug 语句上暂停。例如,
Debug.Assert Var >= 0
这将暂停在Debug.Assert
声明如果Var >= 0
is False
;也就是说,如果Var
是负数。当条件为 False 而不是 True 时暂停执行,这似乎是倒退的,但 Assert 方法是从 C 语言中采用的,其用法与 C 中相同。
请务必删除或注释掉Debug.Print
and Debug.Assert
调试完成后的语句。您通常不希望这些语句在应用程序的正常使用期间起作用。
当地人之窗
当您单步执行过程时,“局部变量”窗口允许您查看过程中所有变量的值。要显示本地窗口,请选择当地人之窗来自View菜单。使用“局部”窗口比检查“立即”窗口中的值更容易显示变量值。对于简单变量类型(例如,长整型变量和字符串变量),值显示在一行上。对于复杂类型或对象(例如 Range 变量),其属性显示在可折叠的树状结构中。
观察窗
监视窗口显示所有有效的监视。您可以通过选择来显示监视窗口观察窗来自View菜单。监视是 VBA 的一条指令,用于在表达式为 True 或正在监视的变量更改值时暂停代码。要在变量上创建监视,请打开“监视”窗口并在“监视”窗口中右键单击并选择添加手表...从弹出菜单中或选择添加手表...从调试窗口。在“添加监视”对话框中,在“表达式”文本框中输入要监视其值的变量名称。然后选择当值改变时中断。当您运行代码时,执行将在修改变量值的行之后暂停。当代码暂停时,变量的值将已经更新。
要删除手表,请在“手表”窗口中右键单击它,然后选择删除观察从弹出菜单中。要修改监视,请在监视窗口中右键单击它并选择编辑手表从弹出菜单中。
调用堆栈
调用堆栈是由 VBA 维护的数据结构,用于跟踪哪个过程调用了另一个过程。例如,如果程序AAA
calls BBB
哪个调用CCC
,“调用堆栈”窗口将显示从最近的过程开始的过程列表,其下方是为到达当前位置而执行的过程链。您可以通过选择查看调用堆栈调用栈来自View菜单。这对于跟踪在当前位置结束的执行流程很有用。不幸的是,没有编程方法可以从调用堆栈中获取信息。
Source: Chip Pearson at Pearson Software Consulting: http://www.cpearson.com/Excel/DebuggingVBA.aspx http://www.cpearson.com/Excel/DebuggingVBA.aspx