Hi friends,
I would like to share some of tips related to the debugging of Visual Basic for Application (VBA) Code. Hope that someone gets help from this.!! The general procedure for debugging Visual Basic for Applications is to divide the processes into sub-large smaller procedures to ensure that the individual steps of each sub-procedure work correctly, then combine the sub-procedures one after the other until all the code works correctly.
Using the Debug window : You can use the Immediate pane of the Debug window to run individual lines of Visual Basic for Applications or to check the values of variables. In the Debug window, you can test and debug a function and sub-procedures to verify the value of a field, control of a property setting of a variable or of expression. Finally, you can view the result of an expression when executing code. For an example on how to use the Debug window, follow these steps :
- Open your function module in Design view.
- In the View menu, click Debug Window.
- Run your function in the pane of the Debug window by inserting the following line and then pressing ENTER:
- ? <FunctionName> ()
- where <FunctionName> is the name of your function. Be sure to include the parenthesis in all arguments expected by your office.
Using a break-point to suspend execution of code : When you suspend the execution of a Visual Basic for Applications, the code is not canceled, but the pauses between the executing instruction. For Visual Basic for Applications pause in the execution of your code, you can insert a breakpoint. For example, if the first part of your code works correctly, but this is not the case for other parts, you can find one that does not work correctly as follows :
- To insert a breakpoint immediately after the section of code that works, move the insertion point of the first line of your code where you want to place a breakpoint.
- Click Toggle Breakpoint in the Debug menu (or the Run menu in version 7.0.). The line appears in bold, which indicates a breakpoint set on this line.
- Run the function in the Debug window. When code execution reaches the breakpoint, processing is suspended and the line containing the breakpoint is selected. You can then check the value of variables in the code.
- The value username appears in the Debug window. If the value is correct, check the other variables. If the value is incorrect, check the previous lines of code to see where this variable has received an incorrect value.
Bookmarks