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)
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.
Then press CTRL + G on your keyboard. The Immediate window will open.
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.
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.
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.
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
- Excel VBA: Print Preview for Selected Range
- How to Display Print Preview with Excel VBA
- Excel VBA: How to Set Print Area Dynamically
- How to Set Print Area to Selection Using VBA in Excel
- Excel VBA: Set Print Area for Multiple Ranges
- VBA Code for Print Button in Excel
- Excel VBA: Print Range of Cells
- How to Print Specific Sheets Using VBA Macro in Excel
- Excel VBA to Display Print Preview for Multiple Sheets