调试VBA,定位问题&故障排除方法 [英] Debugging VBA, Locating Problems & Troubleshooting Methods

查看:26
本文介绍了调试VBA,定位问题&故障排除方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调试 VBA 代码有哪些方法?

特别是:

  • 单步执行代码
  • 断点和停止命令
  • Debug 命令
  • 当地人和观察窗口
  • 调用栈

推荐答案

调试 VBA 代码

本页介绍了调试 VBA 代码的方法.

Debugging VBA Code

This page describes methods for debugging your VBA code.

调试程序是软件开发中最重要的步骤之一.了解 VBA 的调试工具可以使调试更轻松、更高效.本页介绍了在测试和调试应用程序时可以使用的几种 VBA 内置调试​​工具.

Debugging a program is one of the most important steps in software development. Knowledge of VBA's debugging tools can make debugging easier and more productive. This page describes several of VBA's built-in debugging tools you can use when testing and debugging your application.

调试代码的首要方法之一是一次一行地单步调试代码.要单步执行代码,请将光标放在要分析的第一行代码上,然后按 F8 或在 调试 菜单上选择 Step Into.下一行要执行的代码将以黑色字体显示在黄色背景中.请注意,突出显示的代码行尚未执行——它是要执行的下一行.

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.

如果您的代码调用另一个过程,使用 F8 单步执行代码将导致执行以逐行顺序进入被调用的过程.如果您想不单步执行被调用的过程,请按Shift+F8.这将执行被调用的过程,然后在调用该过程后在代码行上暂停.如果您已经在单步执行某个过程,则可以按 Ctrl+ F8 以逐行恢复代码执行.在分步模式或断点处(见下文)暂停时,您可以在 运行<中按 F5继续/strong> 菜单使 VBA 运行完成或直到遇到暂停语句.

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.

每当您在逐步模式中暂停时,您都可以从立即窗口查询或更改变量的值.

Whenever you are paused in step-by-step mode, you can query or change a variable's value from the Immediate window.

断点是放置在一行代码上的标记,它会在执行该行之前立即暂停执行.您可以通过将光标放在相关代码行上并按 F9,在 Debug 上选择 Toggle Breakpoint 来向代码行添加断点strong> 菜单,或单击代码行旁边的左边距.设置断点后,该行以砖红色背景显示,字体为白色.当您运行代码时,执行将在带有断点的代码行之前立即暂停,并以黑色字体以黄色背景显示.请注意黄色的那一行还没有被执行——它是下一行要运行的代码.

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.

当代码在断点处暂停时,您可以在立即窗口中发出命令来更改或查询变量的值.要查看变量的内容,请输入 ?字符后跟变量名称,然后按 Enter.您可以通过在立即窗口中输入 VariableName = NewValue 并按 Enter 来更改变量的值.

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.

如果立即窗口不可见(通常在 VBA 编辑器屏幕的底部),请按 Ctrl+G 或选择 立即窗口查看菜单使窗口可见.

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.

要删除断点,请将光标放在代码行上并按F9.您可以通过从调试菜单中选择清除所有断点或按Ctrl+Shift+F9.VBA 还提供了 Stop 命令.这只会停止该行代码的执行并进入中断模式.

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.

完成代码调试后,请务必返回并清除所有断点(从调试菜单中选择清除所有断点或按 Ctrl+Shift+F9) 并确保删除或注释掉所有停止语句.

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.

当你在断点处或分步模式下暂停时,你可以改变下一行要执行,要么在当前行之前重新运行一段代码,要么在该行之后跳过语句.右键单击要恢复执行的行,然后右键单击并选择 Set Next Statement 或从 Run 菜单中选择 Set Next Statement.将在选定的代码行处继续执行.

When you are paused at a breakpoint or in step-by-step mode, you can change the next line to be executed, either before the current line to re-run a section of code, or after the line to skip statements. Right-click the line where you want execution to resume and right-click and choose Set Next Statement or choose Set Next Statement from the Run menu. Execution will resume at the selected line of code.

VBA 提供了一个带有两个属性 Print 和 Assert 的 Debug 对象,您可以使用它们来显示变量的值并控制程序流程.Debug.Print 会将它后面的内容写入立即窗口.代码执行不会中断.在立即窗口中显示文本后,代码执行继续运行.您可以在 Debug.Print 语句中混合文字文本和变量名称.例如,

VBA provides a Debug object with two properties, Print and Assert that you can use display a variable's value and to control the program flow. Debug.Print will write what follows it to the Immediate window. Code execution is not interrupted. After displaying the text in the Immediate window, code execution continues to run. You can mix literal text with variable names in the Debug.Print statement. For example,

Debug.Print "The value of variable X is: " & X

您可以在立即窗口中同时显示多个变量,方法是用逗号分隔它们.例如,

You can display several variables at once in the Immediate window by separating them with commas. For example,

Debug.Print X, Y, Z

Debug.Assert 命令是一个条件断点,如果 Assert 语句后面的表达式为 False,它将导致执行在 Debug 语句上暂停.例如,

The Debug.Assert command is a conditional breakpoint that will cause execution to pause on the Debug statement if the expression that following the Assert statement is False. For example,

Debug.Assert Var >= 0

如果 Var >= 0False,这将在 Debug.Assert 语句上暂停;也就是说,如果 Var 为负,它将暂停.当条件为False而不是True时暂停执行可能看起来倒退,但Assert方法是从C语言中采用的,其用法与C中保持一致.

This will pause on the Debug.Assert statement if Var >= 0 is False; that is, it will pause if Var is negative. It may seem backwards that execution is paused when the condition is False rather than True, but the Assert method was adopted from the C language, and its usage remained the same as in C.

请务必在完成调试后删除或注释掉 Debug.PrintDebug.Assert 语句.您通常不希望这些语句在您的应用程序正常使用期间起作用.

Be sure to remove or comment out the Debug.Print and Debug.Assert statements when you are finished debugging. You generally don't want these statements to be operative during normal usage of your application.

局部变量窗口允许您在逐步执行过程时查看过程中所有变量的值.要显示 Locals 窗口,请从 View 菜单中选择 Locals Window.使用本地窗口显示变量值比检查立即窗口中的值更容易.对于简单的变量类型(例如 Long 和 String 变量),值显示在一行上.对于复杂类型或对象(例如,Range 变量),其属性以可折叠的树状结构显示.

The Locals windows allows you to view the value of all the variables in a procedure when you are stepping through the procedure. To display the Locals window, choose Locals Window from the View menu. Using the Locals window is easier to display variable values than examining the value from the Immediate window. For simple variable types (e.g., Long and String variables), the value is displayed on one line. For complex types or objects (e.g., a Range variable), its properties are displayed in a collapsible tree-like structure.

Watch 窗口显示所有有效的 Watches.您可以通过从查看菜单中选择观看窗口来显示观看窗口.Watch 是 VBA 的指令,用于在表达式为 True 或正在监视的变量更改值时暂停代码.要在变量上创建监视,请打开监视窗口并在监视窗口中右键单击并从弹出菜单中选择添加监视... 或选择添加监视...em> 从调试窗口.在添加监视"对话框中,在表达式"文本框中输入要监视其值的变量名称.然后选择值变化时中断.当您运行代码时,执行将在修改变量值的行之后的行处暂停.当代码暂停时,变量的值已经被更新了.

The Watch window displays all the Watches in effect. You can display the Watch window by choosing Watch Window from the View menu. A Watch is an instruction to VBA to pause code when an expression is True or when the variable being watched changes value. To create a Watch on a variable, open the Watch window and right-click in the Watch window and choose Add Watch... from the popup menu or choose Add Watch... from the Debug windows. In the Add Watch dialog, enter in the Expression text box a variable name whose value you want to watch. Then choose Break When Value Changes. When you run the code, execution will pause at the line after the line that modifies the variable's value. When code pauses, the value of the variable will have already been updated.

要删除监视,请在监视窗口中右键单击它,然后从弹出菜单中选择删除监视.要修改 Watch,请在 Watch 窗口中右键单击它,然后从弹出菜单中选择 Edit Watch.

To remove a Watch, right-click it in the Watch window and choose Delete Watch from the popup menu. To modify a Watch, right-click it in the Watch window and choose Edit Watch from the popup menu.

调用堆栈是由 VBA 维护的数据结构,用于跟踪哪个过程调用了另一个过程.例如,如果过程AAA 调用BBB 调用CCC,则调用堆栈窗口将显示从最近的过程开始的过程列表和下面是为到达当前位置而执行的程序链.您可以通过从查看菜单中选择调用堆栈来查看调用堆栈.这对于跟踪在当前位置结束的执行流程很有用.不幸的是,没有从调用堆栈中获取信息的编程方式.

The Call Stack is a data structure maintained by VBA that tracks which procedure called another procedure. For example, if procedure AAA calls BBB which calls CCC, the Call Stack window will display the list of procedures starting with the most recent procedure and below that, the chain of procedures that were executed to get to the current position. You can view the Call Stack by choosing Call Stack from the View menu. This is useful to track the flow of execution that ended up in the current location. Unfortunately, there is no programmatic way to get information from the call stack.

来源:Pearson Software Consulting 的 Chip Pearson:http://www.cpearson.com/Excel/DebuggingVBA.aspx

这篇关于调试VBA,定位问题&amp;故障排除方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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