Debugging Techniques for Excel 2016 VBA
You may run into some problems with your VBA code, but how do you find the problem? Sometimes your VBA code may need some debugging. Keep reading to discover the four most common methods for debugging Excel VBA code:
Examining the code
Inserting MsgBox functions at various locations in your code
Inserting Debug.Print statements
Using the Excel built-in debugging tools
Examining your code
Perhaps the most straightforward debugging technique is simply taking a close look at your code to see whether you can find the problem. This method, of course, requires knowledge and experience. In other words, you have to know what you’re doing. If you’re lucky, the error jumps right out, and you slap your forehead and say, “D’oh!” When the forehead pain diminishes, you can fix the problem.
Notice the use of the words, “If you’re lucky.” That’s because often you discover errors when you have been working on your program for eight hours straight, it is 2 a.m., and you are running on caffeine and willpower. At times like that, you are lucky if you can even see your code, let alone find the bugs. Thus, don’t be surprised if simply examining your code isn’t enough to make you find and expunge all the bugs it contains.
Using the MsgBox function
A common problem in many programs involves one or more variables not taking on the values you expect. In such cases, monitoring the variable(s) while your code runs is a helpful debugging technique. One way to do this is by inserting temporary MsgBox functions into your routine. For example, if you have a variable named CellCount, you can insert the following statement:
When you execute the routine, the MsgBox function displays CellCount’s value.
It’s often helpful to display the values of two or more variables in the message box. The following statement displays the current value of two variables: LoopIndex (1) and CellCount (72), separated by a space.
MsgBox LoopIndex & “ “ & CellCount
Notice that the two variables are combined with the concatenation operator (&) and insert a space character between them. Otherwise, the message box strings the two values together, making them look like a single value. You can also use the built-in constant, vbNewLine, in place of the space character. vbNewLine inserts a line-feed break, which displays the text on a new line. The following statement displays three variables, each on a separate line:
MsgBox LoopIndex & vbNewLine & CellCount & vbNewLine & MyVal
This technique isn’t limited to monitoring variables. You can use a message box to display all sorts of useful information while your code is running. For example, if your code loops through a series of sheets, the following statement displays the name and type of the active sheet:
MsgBox ActiveSheet.Name & “ “ & TypeName(ActiveSheet)
If your message box shows something unexpected, press Ctrl+Break, and you see a dialog box that tells you Code execution has been interrupted, you have four choices:
Click the Continue button. The code continues executing.
Click the End button. Execution stops.
Click the Debug button. The VBE goes into Debug mode.
Click the Help button. A help screen tells you that you pressed Ctrl+Break. In other words, it’s not very helpful.Pressing Ctrl+Break halts execution of your code and gives you some choices.
If your keyboard doesn’t have a Break key, try pressing Ctrl+ScrollLock.
Feel free to use MsgBox functions frequently when you debug your code. Just make sure that you remove them after you identify and correct the problem.
Inserting Debug.Print statements
As an alternative to using MsgBox functions in your code, you can insert one or more temporary Debug.Print statements. Use these statements to print the value of one or more variables in the Immediate window. Here’s an example that displays the values of three variables:
Debug.Print LoopIndex, CellCount, MyVal
Notice that the variables are separated with commas. You can display as many variables as you like with a single Debug.Print statement.
Debug.Print sends output to the Immediate window even if that window is hidden. If VBE’s Immediate window is not visible, press Ctrl+G (or choose View → Immediate Window). Here’s some output in the Immediate window.
Unlike MsgBox, Debug.Print statements do not halt your code. So you need to keep an eye on the Immediate window to see what’s going on.
After you’ve debugged your code, be sure to remove all the Debug.Print statements. Even big companies like Microsoft occasionally forget to remove their Debug.Print statements. In several previous versions of Excel, every time the Analysis ToolPak add-in was opened, you’d see several strange messages in the Immediate window. That problem was finally fixed in Excel 2007.
Using the VBA debugger
The Excel designers are intimately familiar with the concept of bugs. Consequently, Excel includes a set of debugging tools that can help you correct problems in your VBA code.