Excel VBA Debug Print: How to Do It?

Get FREE Advanced Excel Exercises with Solutions!

In this article, I’ll show you how you can debug print in VBA in Excel. You’ll learn to open the immediate window and debug print a complete code, a line of code, or the value of a particular variable in the window.


Excel VBA Debug Print (Quick View)

Debug Print in Excel VBA


How to Perform Debug Print in Excel VBA: 4 Effective Ways

So, no more delay. Let’s see how we can debug print through VBA in Excel.

1. Open the Immediate Window to Debug Print in Excel VBA

In order to debug print in VBA, first, you have to open the Immediate window. To do that, press ALT + F11 to open the Visual Basic window.

VBA Window to Debug Print in Excel VBA

Then press CTRL + G on your keyboard. The Immediate window will open.

Immediate Window to Debug Print in Excel VBA


2. Debug Print a Complete Code in the Immediate Window in Excel VBA

Now we’ve learned to open the Immediate window. Let’s see how can debug print the output of a complete VBA code in the window.

Let’s have a VBA code that takes two integers as variables A and B, and returns their sum in variable C.

Sub Debug_Print()

A = 10

B = 15

C = A + B

End Sub

Now to print the value of C in the Immediate window, you have to insert this line.

Debug.Print C

Debug Print in Excel VBA

Now, if you run the code, you’ll find the value of C (10+15 = 25) in the Immediate window.

Debug Print in Excel VBA


3. Debug Print a Line of Code in the Immediate Window in VBA

In the previous example, we saw how we can print the output of a code in the Immediate window by the debug.print command.

But you can directly write a line of code in the Immediate window and run it. For example, here I’ve written this line of code in the Immediate window.

Debug.Print "This is Immediate Window."

Now press ENTER on your keyboard. The line will be executed and you’ll find the output in the next line of the window.

Writing Code in Immediate Window in VBA to Debug Print


4. Debug Print in Break Mode in the Immediate Window in Excel VBA

You can also debug print the value of a particular code in the Immediate window. But to do that, you have to go to the break mode of VBA.

For example, in the following figure, the code is in the break mode from in the line:

C = A + B

Break Mode Debug Print in Excel VBA

Break mode means, while running the code, that particular line won’t be executed.

Now, we can debug print the value of any variable in the Immediate window.

For example, to print the value of the variable B, insert this line of code in the Immediate window:

Click ENTER. And you’ll find out the value of B displayed. That’s 15.


Things to Remember

Debug Print is nothing but displaying a particular value in VBA in the Immediate window. In case, you don’t want to use debug print, you can use the Message Box in VBA.


Download Practice Workbook

Download this practice workbook while you are reading this article.


Conclusion

So, these are the methods to use debug print in VBA. Do you have any questions? Feel free to ask us.


Related Articles

What is ExcelDemy?

ExcelDemy - Learn Excel & Get Excel Solutions Center provides online Excel training , Excel consultancy services , free Excel tutorials, free support , and free Excel Templates for Excel professionals and businesses. Feel free to contact us with your Excel problems.

Tags:

Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo