The information in this article applies to:
SUMMARY
The Microsoft Visual Basic Editor provides a rich set of debugging tools
that you can use to troubleshoot problems with your Visual Basic for
Applications macro code. This article describes some general debugging
techniques that you can use to debug Visual Basic for Applications code.
MOREINFOMicrosoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspYou can use several methods to debug Visual Basic for Applications code. Some of the commonly used techniques are described in this article. USING A MESSAGE BOX TO DEBUG CODEThe most basic method to check the value of a variable at a specific step in the macro is to display that value in a message box. A message box that you use for debugging should be removed after you finish debugging.To use a message box for debugging, enter the MsgBox function code immediately after the line of code that uses the variable you want to check. For example, use the following syntax
where <NameOfVariable> is the name of the variable you want to check.
The following sample macro displays a message box every time the value of variable "i" changes:
For more information about the MsgBox function, click the Office Assistant
in the Visual Basic Editor, type "MsgBox" (without the quotation marks),
click Search, and then click to view the "MsgBox Function" topic.
NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base: Q120802 Office: How to Add/Remove a Single Office Program or Component USING BREAKPOINTSYou can set a breakpoint to suspend a macro that is running at a specific statement in the macro. Typically, you set a breakpoint where you suspect a problem exists. You clear breakpoints when you no longer need them to stop the macro.Setting BreakpointsTo set a breakpoint, use either of the following methods.Method 1:
Clearing Breakpoints
USING BREAK MODEWhen your code pauses, for example, when it encounters a breakpoint, the macro is in break mode. Break mode allows you to view the current condition of the macro. When a macro is in break mode, you can look at the values of your variables. You can also step through the code one line at a time to trace the logic of the macro.Entering Break ModeOne method for entering break mode involves stepping through code from the beginning of the macro. To do this, follow these steps:
Exiting Break ModeTo exit break mode, click Reset on the Run menu. This ends the break mode session.Using ToolTipsWhen you use break mode, ToolTips indicate the current value of a specified variable.To see a sample value displayed in a ToolTip, follow these steps:
NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base: Q120802 Office: How to Add/Remove a Single Office Program or Component USING THE IMMEDIATE WINDOWThe Immediate window displays information that results from debug statements in your code or from commands that you type directly in the Immediate window. In the Immediate window, you can evaluate most valid Visual Basic for Applications statements.Displaying the Immediate WindowTo display the Immediate window, click Immediate Window on the View menu or press CTRL+G.Using the Debug ObjectUse the Debug object to print information in the Immediate window. You can use one method with the Debug object. This method is the Print method. The following example uses the Debug object and the Print method:
For more information about the Debug Object, click the Office Assistant in
the Visual Basic Editor, type "debug" (without the quotation marks), click
Search, and then click to view the "Debug Object" topic.
Clearing the Immediate WindowThe text that is printed in the Immediate window using the Debug.Print statement or text that you type directly into the Immediate window is not cleared when the macro is finished. This behavior is by design. You can check the output that appears in the Immediate window after you run the macro.To clear the Immediate window, follow these steps:
Entering Commands in the Immediate WindowYou can type commands directly in the Immediate window. For example, to determine the name of an Office program, type the following text in the Immediate window
and press ENTER.
After you press ENTER, information that is similar to the following appears:
NOTE: You must be in break mode to enter statements in the Immediate
window.
For more information about the Immediate Window, click the Office Assistant in the Visual Basic Editor, type "immediate window" (without the quotation marks), click Search, and then click to view the "Use the Immediate Window" topic. USING THE LOCALS WINDOWWhen you use break mode, the Locals window automatically displays the values and types for all declared variables in the current procedure.The following example uses the Locals window. To use the window, use the following steps:
USING THE WATCH WINDOWThe Watch window also allows you to monitor the values of your variables. Unlike the Locals window, values are not automatically populated; you must manually add the values that appear in the Watch window.NOTE: You can add values to the Watch window only when you are using break mode; therefore, you must be using break mode to use the following methods. To add a variable to the Watch window, use either of the following methods. Method 1
Method 2
USING THE CALL STACKIn break mode you can use the call stack to display a list of currently active procedure calls. When you execute code in a procedure, that procedure is added to a list of active procedure calls. Each time a procedure calls another procedure, it is added to the list. When control is returned to the calling procedure, called procedures are removed from the list. Procedures called from the Immediate window are also added to the calls list.USING CONDITIONAL COMPILATIONYou can use conditional compilation to selectively run blocks of code. The following sample macro uses conditional compilation:
The behavior of the #If...Then...#Else conditional compilation directive is
the same as the If...Then...Else statement. However, code that is excluded
during conditional compilation is completely omitted from the final
executable file; so, using conditional compilation has no size or
performance disadvantages.
Additional query words: 8.00 ppt8 vba vbe kbwordvba xlvbainfo 98 macppt
Keywords : kbcode kbprg DBugDebugW DBugErrhand |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |