|
|
| Back to Excel Homepage | Excel VBA - Reference Guide |
|
Menu Debugging
Other links
|
Debugging is the process of stepping through the code line by line and checking the reaction of each line to help trace errors that may be difficult to find at run time especially logical errors. The Debug toolbar allows users to step in, out, over or watch certain variables change state in a controlled manner and can be switched on or off in the Visual Basic Editor window.
The most effective way to debug a procedure is to learn some keystrokes and mark breakpoints in the code. To add breakpoints, place the mouse pointer to the left grey margin at the point where you wish to pause the procedure and click once with the left mouse button, click button 5 (as above) or press F9 function key (toggles on/off).
When you run the procedure or press the F5 key, the procedure will pause at the first highlighted break:
At this point, users can either continue to run the remaining procedure (press F5 key) or step through line by line by pressing the F8 key. By placing the mouse pointer over any variable or object property, the user will, after a few seconds, see the current value assigned. Alternatively, by revealing the Locals Window (button 9 above), users can see all variables and property’s values:
After a few steps (F8 key):
Debugging between calling procedures can be controlled as the F8 key steps in order line by line across more than one procedure. To step out of a sub procedure and carry on with the main procedure, press the SHIFT + F8 keys. Debug.Print CommandA return value will be printed to the Immediate Window (button 10 above or CTRL + G). Two ways to print an output value in the immediate window:
The above will print the ‘hours’ variable to the immediate window:
If you set a breakpoint and have the Immediate Window visible, you can use a different method to reveal the current values of any known variable or property:
Type a question mark ( ? ) followed by a space and then the variable or property and press the enter key to reveal the output value.
|
Tip: Keyboard shortcuts are quick and simple. Learn F5, F8 and F9.VBA Keywords: On Error GoTo, MsgBox, InputBox, Debup.Print & CCur. |
|
© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved. All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy. |