VBA’s Very Handy but Little Known Debug.Print

This entry is going to be quite short.

Tired of endless Message Boxes when trying to discover the flaw in your code? Try the wonderful but surprisingly little known VBA command Debug.Print. This is a VBA command that I have used for years. I was recently surprised to discover that several of my friends who use VBA professionally did not know about it.
When debugging your VBA code, it always nice to follow the values of your variables and functions to try to discover the source of the problem. There are many ways to do this.
Message Boxes (Command: msgbox) have their uses but if you are trying to track the value of a loop counter they become extremely annoying very quickly. Having to click OK 40 times really gets on one’s nerves.

Say hello to Debug.Print

It’s an extremely simple command. Anywhere in your code, just type “Debug.Print” and the variable or function that you want afterwards. You can even customize it with a string.

Example

Sub TestDebugDotPrint()
Dim lngTroublesomeVar as long
lngTroublesomeVar = 22
Debug.Print “The lngTroublesomeVar is: “ & lngTroublesomeVar
End Sub

If you look at the immediate window in the VBA IDE

(if you can’t see it, click View->Immediate Window or type Ctrl-G),

you should see:

The lngTroublesomeVar is: 22

Use it for all of your debugging needs!

One thought on “VBA’s Very Handy but Little Known Debug.Print

Leave a Reply to Jon Cancel reply

Your email address will not be published. Required fields are marked *