Common methods for debugging VBA programs

Source: Internet
Author: User

In break mode (Ctrl+break key), you can do:

1. Execute tool----Options----Editor----tick "show data hints automatically"

When you hover over a variable or expression with the mouse, a prompt window appears, showing its name and value!

2. View the values of some expressions in the Watch window by dragging black with the mouse to select the expression, then debugging----adding monitoring, and the Add Watch dialog box appears.

3. In the Code window, select expression----Press SHIFT+F9----The Quick Watch dialog box appears, showing the context, expression, and value of the expression.

4. Press Ctrl+g and the "Immediate Window" appears. It has 4 main functions:

(1). Printing information

Enter Debug.Print expression 1 in the window, Expression 2 ... to display the values of each expression!

2. You can assign a value to a variable or property!

Example: form1.backcolor=255

Maxrows=50

3. Can test the process

In this window you can enter the procedure name and arguments directly to invoke the procedure.

For example:

MySub 2*i

4. You can check the error number

For example: Enter

Error 11

When you press ENTER, the information for error 11th is displayed in this window. I use this method to check the error number!

Breakpoint

Breakpoints can be set in both design and break modes, and when the program executes to a breakpoint statement, it breaks execution and is in break mode!

1. Set breakpoints

In the Code window, click on the left area with your mouse to make it appear black dots, which means you have set breakpoints! You can also move the cursor over the selected statement and press F9.

The breakpoint statement is in bold, and the color can be set by: Tools---options---Editor format---code color----breakpoint Point text option!

2. Undo Breakpoint:

Debug----Clear All breakpoints (CTRL+SHIFT+F9)

3. Using the Stop statement

When you close the project, the breakpoint is not saved and needs to be reset the next time you debug.

If you debug multiple times, the best way is to use the Stop statement, and whenever you encounter it, the program goes into break mode.

The Stop statement can be saved in the code and can be canceled after the test is complete!

The Watch window can monitor expressions, and the QuickWatch window can also display the value of an expression.

The Immediate window evaluates the expression and assigns a value to the variable and property.

The local window displays the current value of the local variable. For example, your own defined variables:

To step through the debugger:

1. Debug----Step by step (pressing shortcut key is F8)

2. In the execution of the statement by the process, the process of encountering the call, do not want to enter into which to stay, press SHIFT+F8, you can "process" execution without entering!

3. Jump out of the process

Debug----Jump Out (the shortcut is Ctrl+shift+f8), you can run the remaining statement code of the current procedure, go to the next statement calling the procedure statement go!

4. Execute a specific block of statements

This is a very important method, commonly used to: In break mode, move the cursor to the line of statements to be executed, perform debugging---Set the next statement,

Use it to set the next statement to execute. Then move the cursor to the statement you want to stop, execute the debug----run to the cursor (the shortcut is Ctrl+f8), and you can run to that statement!

The advantage of this approach is that you can bypass the block of statements that you do not want to execute (for example, you explicitly know a problematic statement).

However, you can execute the tool----options----Editor----Automatic syntax detection, the Code window input code produces a syntax error, will immediately display an error message!

Call stack: When an application is in break mode, it displays the process that has been called!

1. How to display the dialog box for the call stack: View---Call stack (shortcut key is ctrl+l)

2. Tracking nested procedures

In the VBA process, there is often a call to another or some process, module, form.

You can use the call stack to display the project name, module, or form name and procedure name of the activated process from the bottom up.

To do this: ctrl+l----Select a procedure-----display with the mouse.

At this point, the Code window shows the procedure and the cursor is at the calling statement that is about to invoke the next procedure.

At this point, press (SHIFT+F9) to use the Quick Watch or (ctrl+g) Immediate window to display the relevant variables!

3. Use the call stack to check the ability of the procedure to call itself

Source: http://club.excelhome.net/thread-225648-1-1.html

Common methods for debugging VBA programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.