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)
Download Practice Workbook
Download this practice workbook while you are reading this article.
4 Effective Ways to Perform Debug Print in Excel VBA
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.
Then press CTRL + G on your keyboard. The Immediate window will open.
Related Content: Excel VBA: Print UserForm to Fit on a Page (2 Methods)
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
Now, if you run the code, you’ll find the value of C (10+15 = 25) in the Immediate window.
Read More: VBA Code for Print Button in Excel (5 Examples)
Similar Readings
- Excel VBA: How to Set Print Area Dynamically (7 Ways)
- How to Print Excel Sheet in Full Page (7 Ways)
- Print Excel Sheet in A4 Size (4 Ways)
- How to Set a Row as Print Titles in Excel (4 Methods)
- How to Print Multiple Sheets in Excel (7 Different Methods)
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.
Read More: How to Print Excel Sheet with Lines (3 Easy Ways)
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 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.
Related Content: How to Print Selected Area in Excel on One Page (3 Methods)
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.
Conclusion
So, these are the methods to use debug print in VBA. Do you have any questions? Feel free to ask us. And don’t forget to visit our site ExcelDemy for more posts and updates.
Related Articles
- How to Print Excel Sheet with Table (4 Methods + Tricks)
- Remove Print Titles in Excel (3 Methods)
- How to Set Print Area to Selection Using VBA in Excel (3 Methods)
- Set Print Titles in Excel (2 Methods)
- How to Print Excel Sheet with Header on Every Page in Excel (3 Methods)
- How to Print Graph in Excel (5 Ways)