Debugging INTRODUCTION Logic errors are called bugs. The process of finding and correcting errors is called debugging. A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. But for a large, complex program, the most effective approach for debugging is to use a debugger utility. DEBUGGER Debugger is a program that facilitates debugging. You can use a debugger to Execute a single statement at a time. Trace into or stepping over a method. Set breakpoints. Display variables. Display call stack. Modify variables. WHAT IS VBA'S DEBUGGING ENVIRONMENT? In Excel 2016, VBA's debugging environment allows the programmer to momentarily suspend the execution of VBA code so that the following debug tasks can be done: 1. Check the value of a variable in its current state. 2. Enter VBA code in the Immediate window to view the results. 3. Execute each line of code one at a time. 4. Continue execution of the code. 5. Halt execution of the code. These are just some of the tasks that you might perform in VBA's debugging environment.
This document covers the following: How to set a breakpoint? How to clear a breakpoint? How to clear all breakpoints? Debug mode in VBA How to check values in VBA? How to use the Immediate Window? How to move through the code Step Into Step Over Continue Halt WHAT IS A BREAKPOINT? In Excel 2016, a breakpoint is a selected line of code that once reached, your program will momentarily become suspended. Once suspended, you are able to use VBA's debugging environment to view the status of your program, step through each successive line of code, continue execution of the code, or halt execution of the code.
You can create as many breakpoints in your code as you want. Breakpoints are particularly useful when you want to suspend your program where you suspect a problem/bug exists. SETTING A BREAKPOINT Next, let's look at how to set a breakpoint. First, you need to open the VBA environment. The quickest way to do this is by pressing Alt+F11 while your Excel database file is open. To set a breakpoint, find the line of code where you'd like to suspend your program. Left-click in the grey bar to the left of the code. A red dot should appear and the line of code should be highlighted in red. In this example, we've created a breakpoint at the following line of code: While LPos <= Len(pValue)
CLEARING A BREAKPOINT A breakpoint in VBA is indicated by a red dot with a line of code highlighted in red. To clear a breakpoint in Excel 2016, left-click on the red dot next to the line of code that has the breakpoint. In this example, we want to clear the breakpoint at the following line of code: While LPos <= Len(pValue)
Now, the breakpoint is cleared and the line of code should look normal again. CLEARING ALL BREAKPOINTS Because you can set as many breakpoints as you want in Excel 2016, you can save time by clearing all breakpoints in your VBA code at once. To clear all breakpoints in your program, select "Clear All Breakpoints" under the Debug menu.
This will remove all breakpoints from your VBA code, so that you don't have to individually remove each breakpoint, one by one. DEBUG MODE Now that we know how to set and clear breakpoints in Excel 2016, let's take a closer look at the debug mode in VBA. In our example, we've set our breakpoint and entered our AlphaNumeric function as a formula in a cell. This will cause the VBA code to execute.
When the breakpoint is reached, Excel will display the Microsoft Visual Basic window and highlight the line (in yellow) where the code has been suspended.
Now we are in debug mode in our Excel spreadsheet. Now we can do any of the following: 1. Check the value of a variable in its current state. 2. Enter VBA code in the Immediate window to view the results. 3. Execute each line of code one at a time. 4. Continue execution of the code. 5. Halt execution of the code. CHECKING VALUES In Excel 2016, if you want to view the value of a variable in your VBA code at the time that the program was suspended, you can move your mouse pointer over that VBA code. Bubble text will appear displaying the variable/expression name and its value in its current state.
In this example, we've moved the mouse pointer over the expression Value. The bubble text displays the expression name with a value of "123 Main St". This feature is useful if you need to quickly check a variable/expression in your code. USING THE IMMEDIATE WINDOW In Excel 2016, the Immediate window can be used to debug your program by allowing you to enter and run VBA code in the context of the suspended program. We've found the Immediate window to be the most help when we need to find out the value of a variable, expression, or object at a certain point in the program. This can be done using the print command. For example, if you wanted to check the current value of the variable called pvalue, you could use the print command as follows:
In this example, we typed print len(pvalue) in the Immediate window and pressed ENTER. print len(pvalue) The Immediate window displayed the result of 6 in the next line. The Immediate window can be used to run other kinds of VBA code, but bear in mind that the Immediate window can only be used when debugging so any code that you run is for debugging purposes only. The code entered in the Immediate window does not get saved and added to your existing VBA code. WATCH WINDOW The Microsoft Visual Basic for Applications window displays your VBA environment in Excel 2016:
Next, let's analyze the Watch window in the VBA environment. In Excel 2016, the Watch window is usually found below the Code window. It is one of the most valuable tools when debugging in the VBA environment. It lets you: Define and monitor any expression. When in debug mode, it lets you view the value of the watched expression in its current state. This will be discussed in the tutorial on Debugging VBA Code. If the Watch window is not visible when you open the Microsoft Visual Basic for Applications window, you can make it visible by selecting Watch Window under the View menu.
ADDING A WATCH EXPRESSION In Excel 2016, the Watch Window displays the value of a watched expression in its current state. This can be extremely useful when debugging VBA code. Let's explore how to add an expression to the Watch Window. To add a Watch expression, select Add Watch under the Debug menu.
When the Add Watch window appears, enter the expression to watch and click the OK button when you are done.
In this example, we've entered the following watch expression in the Expression field: Len(pValue) Next, we've selected AlphaNumeric as the Procedure and Module1 as the Module when setting up the Context for the watched expression. Finally, we've selected Watch Expression as the Watch Type but there are 3 options to choose from: Watch Type Watch Expression Break When Value Is True Break When Value Changes Description To display the value of the watched expression in its current state To stop the execution of the code when the value of the watched expression is True To stop the execution of the code when the value of the watched expression changes Now when you return to the VBA window, the Watch Window will automatically appear if it was previously hidden. Within the Watch Window, all of the watched expressions should be listed including the one that we just added.
As you can see, the expression Len(pValue) now appears in the Watch Window with a value of 6. Adding a watch is a great way to keep track of variables or expressions of interest when debugging your VBA code. CHOOSE HOW TO MOVE THROUGH YOUR CODE Your program is now suspended at the highlighted breakpoint. What's next?
Now that you've suspended execution of your program in Excel 2016 and are in debug mode, you need to choose how to proceed through your code. We'll cover the 4 most common choices. 1. Step Into 2. Step Over 3. Continue 4. Halt Let's look at each of these. STEP INTO While in debug mode, you can "Step Into" your VBA code in Excel 2016.
You can choose to "Step Into" your code in Excel 2016. What this means is that you will step through each line of code in your current procedure as well as step into the code of any procedures that are called by the current procedure. You can do this by either pressing the F8 key or selecting "Step Into" under the Debug menu.
Each time you select "Step Into", the debugger will move you to the next line of code. STEP OVER While in debug mode, you can "Step Over" your VBA code in Excel 2016.
You can choose to "Step Over in Excel 2016". What this means is that you will step through each line of code in your current procedure, but step over (ie: run as a unit) any procedures that are called by the current procedure. You can do this by either pressing the Shift+F8 key or selecting "Step Over" under the Debug menu.
Each time you select "Step Over", the debugger will move you to the next line of code in your current procedure. If your current procedure calls another procedure, the debugger will step over the called procedure. It won't drill down into the code of the called procedure. CONTINUE While in debug mode, you can "Continue" your VBA code in Excel 2016.
You can choose to "Continue" execution of your code in Excel 2016. What this means is that your suspended program will continue executing from where it left off. With this option, your program should either finish running or suspend at the next breakpoint encountered. You can do this by either pressing the F5 key or selecting "Continue" under the Run menu.
HALT While in debug mode, you can "Halt" your VBA code in Excel 2016.
You can choose to "Halt" execution of your code in Excel 2016. What this means is that your suspended program will halt execution. You will no longer be in debug mode. You can do this by selecting "Reset" under the Run menu.
You would normally select this option after you've identified the bug in your code. Once your code is halted, you can modify your VBA code to remedy the problem. SOURCE https://www.techonthenet.com/excel/macros/vba_debug2016.php