Errors and Debugging

Other topics about Operation Basics

Type of Errors

Errors in VBA programs are classified into the following two types:

Syntax errors

A syntax error is generated when Visual Basic Editor detects an invalid statement that violates the Visual Basic syntax rules. For example, misspelled keywords generate syntax errors. An error dialog box appears that indicates the error message, and highlight the invalid statement in red. To get detailed information on the error, click the HELP button in the error dialog box to display the help topic on the error. You cannot run the macro until you correct the syntax error.

The E5071C VBA environment is by default configured to automatically check for syntax errors, but you can disable the auto syntax check feature using the following steps:

  1. On the Tools menu, click Options....

  2. On the Editor tab, clear the Auto Syntax Check check box.

  3. Click the OK button.

Run-time Errors

A run-time error is generated when a VBA program attempts to execute an invalid statement at run time. When a run-time error is generated, the program is stopped at the invalid statement, and an error dialog box appears. You can terminate the program by clicking the END button in the error dialog box. Also, you can click the DEBUG button in the error dialog box to identify the statement that caused the error. In this case, the statement in question is highlighted in yellow.

 

Using a Debug Tool

The E5071C's VBA environment provides a variety of debug tools that help you identify logical errors. Detailed information on using the debug tools is covered in VBA Online Help and books on VBA.

Debug Toolbar

The debug toolbar provides tool buttons that allow you to easily access various debug tools.

 On the View menu, click Toolbars > Debug.

Debug toolbar

 

  1. Set/clear break points (keyboard F9): Puts a break point at the cursor position or clears an existing break point.

  2. Step-in (keyboard: F8): Runs the VBA program step by step. If the current program contains a call to another procedure, that procedure is also run step by step.

  3. Step-over (keyboard: Shift + F8): Runs the VBA program step by step. If the current program contains a call to another procedure, that procedure is run as one line.

  4. Step-out (keyboard: Ctrl + Shift + F8): Executes the remaining lines of the function where the execution point is currently placed.

  5. Local window: Opens the local window that shows the current values of local variables.

  6. Immediate window (keyboard: Ctrl + G): Opens the immediate window that evaluates entered values of variables or expressions.

  7. Watch window: Opens the watch window that displays the current value of a specified expression.

  8. Quick window (keyboard: Shift + F9): Displays the current value of a specified expression in a dialog box.

Setting a Break Point

By placing a break point at a particular statement in a VBA program, you can automatically suspend the program when it is executed to that statement.

When you put a break point at a line, the line is highlighted in umber as shown in the following figure. To set a break point, do one of the following:

 Setting a break point

Monitoring Variable or Property Values

With your VBA program suspended, you can use the following debug tool to monitor variables or properties. To do this, you must set a break point, run the VBA program, and suspend it.

Data Hint

When you point to the variable or expression of interest, Data Hint shows the current value as shown in the following figure.

 Data Hint

Immediate Window

To display the immediate window, click on the debug toolbar.

In the immediate window, enter a question mark (?) followed by the variable or expression whose value you want to check, and press the Enter key, as shown in the following figure. The current value appears in the line that follows.

Immediate window

 

Watch Window

To display the watch window, click the "Watch Window" button on the debug toolbar.

Watch window

  1. On the Debug menu, click Add Watch.... to open the Add Watch dialog box.

  2. As shown in the following figure, you can specify an expression of interest as a watch expression to always monitor its value.

  3. Click the OK button.

Add Watch dialog box

Quick Watch

In the code window, select a variable or expression whose value you want to watch. On the debug toolbar, click the "Quick Watch" button to open the Quick Watch dialog box. The dialog box displays the current value of your specified variable or expression.

Also, you can click the Add button in the Quick Watch dialog box to specify the current expression as a watch expression.