You are currently offline, waiting for your internet to reconnect

OFF: Overview of Visual Basic Editor Debugging Tools

This article was previously published under Q165517
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
The Microsoft Visual Basic Editor provides a rich set of debugging toolsthat you can use to troubleshoot problems with your Visual Basic forApplications macro code. This article describes some general debuggingtechniques that you can use to debug Visual Basic for Applications code.

NOTE: This article assumes that you are using the Visual Basic Editor tocreate macro code and that the code compiles correctly but does not performas expected.
MORE INFORMATION
Microsoft 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 a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMSYou 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 CODE

The most basic method to check the value of a variable at a specific stepin the macro is to display that value in a message box. A message box thatyou use for debugging should be removed after you finish debugging.

To use a message box for debugging, enter the MsgBox function codeimmediately after the line of code that uses the variable you want tocheck. For example, use the following syntax
   MsgBox <NameOfVariable>				
where <NameOfVariable> is the name of the variable you want to check.

The following sample macro displays a message box every time the value ofvariable "i" changes:
   Sub MsgBoxCheck()      Dim i      For i = 1 to 5        MsgBox i      Next i   End Sub				
For more information about the MsgBox function, click the Office Assistantin 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 theStandard toolbar. If Visual Basic for Applications Help is not installed onyour computer, please see the following article in the Microsoft KnowledgeBase:
120802Office: How to Add/Remove a Single Office Program or Component

USING BREAKPOINTS

You can set a breakpoint to suspend a macro that is running at a specificstatement in the macro. Typically, you set a breakpoint where you suspect aproblem exists. You clear breakpoints when you no longer need them to stopthe macro.

Setting Breakpoints

To set a breakpoint, use either of the following methods.

Method 1:

  1. Click anywhere in the line of code at which you want the macro to halt.

    A flashing pointer appears.
  2. On the Debug menu, click Toggle Breakpoint (or press F9).

    A breakpoint is defined for that line of code (provided that a breakpoint was not previously defined for the line of code).
Method 2:

  1. Right-click the line of code that contains the breakpoint.
  2. On the shortcut menu, point to Toggle, and then click Breakpoint.
When you run this code, the macro halts on the first line of code thatcontains a breakpoint. Note that you can use more than one breakpoint in amacro.

Clearing Breakpoints

  1. Click anywhere in the line of code that contains the breakpoint you want to remove.
  2. On the Debug menu, click Toggle Breakpoint (or press F9). The breakpoint is removed from the line of code.
NOTE: Breakpoints are not saved with your code. So, you must configurebreakpoints for every debugging session.

USING BREAK MODE

When your code pauses, for example, when it encounters a breakpoint, themacro is in break mode. Break mode allows you to view the current conditionof the macro. When a macro is in break mode, you can look at the values ofyour variables. You can also step through the code one line at a time totrace the logic of the macro.

Entering Break Mode

One method for entering break mode involves stepping through code from thebeginning of the macro. To do this, follow these steps:

  1. Click the line of code that contains the first instruction of the macro.
  2. On the Debug menu, click Step Into.

    An arrow appears to the left of the first line of code.
  3. On the Debug menu, click Step Over.

    This step advances the arrow to the next instruction. If the instruction that is highlighted is a procedure (function, sub, or property), using the Step Over command executes the procedure as a unit without stepping through the code.

    To step through a procedure line by line, click Step Into.

    NOTE: The Debug menu commands are also available on the Debug toolbar.

Exiting Break Mode

To exit break mode, click Reset on the Run menu. This ends the break modesession.

Using ToolTips

When you use break mode, ToolTips indicate the current value of a specifiedvariable.

To see a sample value displayed in a ToolTip, follow these steps:

  1. In a Visual Basic module, type the following sample subroutine:
          Sub Test()         Dim Name As String         Name = "Kerry"         Name = "Nancy"      End Sub						
  2. On the Debug menu, click Step Into (or press F8) to step through the subroutine.

    Click Step Into again until the Name = "Kerry" line is highlighted, and then move the pointer over the Name variable.

    A ToolTip appears with the following text:
          Name = ""						
  3. Press F8 to execute the line of code that assigns the Name variable.

    The Name = "Nancy" line should be highlighted.
  4. Move the pointer over the Name variable. A ToolTip appears with the following text:
          Name = "Kerry"						
For more information about break mode, click the Office Assistant in theVisual Basic Editor, type "Step" (without the quotation marks), clickSearch, and then click to view the "Step Into, Step Over, and Step OutCommands (Debug Menu)" topic.

NOTE: If the Assistant is hidden, click the Office Assistant button on theStandard toolbar. If Visual Basic for Applications Help is not installed onyour computer, please see the following article in the Microsoft KnowledgeBase:
120802Office: How to Add/Remove a Single Office Program or Component

USING THE IMMEDIATE WINDOW

The Immediate window displays information that results from debugstatements in your code or from commands that you type directly in theImmediate window. In the Immediate window, you can evaluate most validVisual Basic for Applications statements.

Displaying the Immediate Window

To display the Immediate window, click Immediate Window on the View menu orpress CTRL+G.

Using the Debug Object

Use the Debug object to print information in the Immediate window. You canuse one method with the Debug object. This method is the Print method. Thefollowing example uses the Debug object and the Print method:
   Sub DebugTest()      ' Prints a string in the Immediate window.      Debug.Print "This text is written in the Immediate window"      ' Prints a floating point and an integer value.      Debug.Print 2.3, 5      ' Outputs text and numbers.      Debug.Print "Print the number " & 2 & "."   End Sub				
For more information about the Debug Object, click the Office Assistant inthe Visual Basic Editor, type "debug" (without the quotation marks), clickSearch, and then click to view the "Debug Object" topic.

Clearing the Immediate Window

The text that is printed in the Immediate window using the Debug.Printstatement or text that you type directly into the Immediate window is notcleared when the macro is finished. This behavior is by design. You cancheck the output that appears in the Immediate window after you run themacro.

To clear the Immediate window, follow these steps:

  1. Click the Immediate window.
  2. On the Edit menu, click Select All.
  3. On the Edit menu, click Clear or Cut.

Entering Commands in the Immediate Window

You can type commands directly in the Immediate window. For example, todetermine the name of an Office program, type the following text in theImmediate window
   ?Application.Name				
and press ENTER.

After you press ENTER, information that is similar to the followingappears:
   ?Application.Name   Microsoft PowerPoint				
NOTE: You must be in break mode to enter statements in the Immediatewindow.

For more information about the Immediate Window, click the Office Assistantin the Visual Basic Editor, type "immediate window" (without the quotationmarks), click Search, and then click to view the "Use the Immediate Window"topic.

USING THE LOCALS WINDOW

When you use break mode, the Locals window automatically displays thevalues and types for all declared variables in the current procedure.

The following example uses the Locals window. To use the window, use thefollowing steps:

  1. In a Visual Basic module, type the following code:
          Sub LocalsTest()         Dim x As Long, y As Long, z As Long         x = 1         y = 2         z = x + y      End Sub						
  2. Step through the code until the "y = 2" line is highlighted. To do this, follow these steps:

    1. Press F8 or click Step Into on the Debug menu.
    2. Press F8 three times so that the y = 2 line is highlighted.
  3. If the Locals window is not visible, click Locals Window on the View menu.
  4. When the "y = 2" line is highlighted, the Locals Window should resemble the following table.
          Expression      Value              Type      ---------------------------------------      x                   1              Long      y                   0              Long      z                   0              Long						
  5. Press F8 again to execute the y = 2 line. View the Locals window. The expression y has the value 2. If you execute the next line (z = x + y), z is assigned the value 3.
  6. Before you execute the next line, change the value of y to 5. To do this, follow these steps:

    1. In the Locals window, click the value of y (2).
    2. Change the value to 5 and press ENTER.
    3. Press F8 again to execute the "z = x + y" line.

      The Locals window should resemble the following table.
               Expression      Value              Type         ---------------------------------------         x                   1              Long         y                   5              Long         z                   6              Long								

USING THE WATCH WINDOW

The Watch window also allows you to monitor the values of your variables.Unlike the Locals window, values are not automatically populated; you mustmanually add the values that appear in the Watch window.

NOTE: You can add values to the Watch window only when you are using breakmode; 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

  1. Right-click the variable you want to add to the Watch window. (You can click any occurrence of the variable.)
  2. On the shortcut menu, click Add Watch.

    The Expression field should display the name of the variable that you clicked in step 1.

    You can also add expressions to the Watch window.

Method 2

  1. On the Debug Menu, Click Add Watch.
  2. Type the expression you want to evaluate and click OK.
For more information about adding expressions to the Watch Window, clickthe Office Assistant in the Visual Basic Editor, type "watch expressions"(without the quotation marks), click Search, and then click to view the"Add a Watch Expression" topic.

USING THE CALL STACK

In break mode you can use the call stack to display a list of currentlyactive procedure calls. When you execute code in a procedure, thatprocedure is added to a list of active procedure calls. Each time aprocedure calls another procedure, it is added to the list. When control isreturned to the calling procedure, called procedures are removed from thelist. Procedures called from the Immediate window are also added to thecalls list.

USING CONDITIONAL COMPILATION

You can use conditional compilation to selectively run blocks of code. Thefollowing sample macro uses conditional compilation:
   Sub Test()      #Const Debugging = 1      Dim Name As String: Name = "Nancy"      ' If you are debugging, change the Debugging constant to 0.      #If Debugging = 0 Then         ' This debug statement is not executed unless the Debugging         ' constant is equal to zero.         Debug.Print Name      #End If      Name = "Kerry"   End Sub				
The behavior of the #If...Then...#Else conditional compilation directive isthe same as the If...Then...Else statement. However, code that is excludedduring conditional compilation is completely omitted from the finalexecutable file; so, using conditional compilation has no size orperformance disadvantages.
8.00 ppt8 vba vbe kbwordvba xlvbainfo 98 macppt
Properties

Article ID: 165517 - Last Review: 01/19/2007 23:04:41 - Revision: 3.5

  • Microsoft Excel 98 for Macintosh
  • Microsoft Word 98 for Macintosh
  • Microsoft PowerPoint 98 for Macintosh
  • Microsoft Word 97 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Office 97 Standard Edition
  • kbcode kbfaq kbinfo kbprogramming KB165517
Feedback