EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Input & Output Functions VBA Debug Print
Secondary Sidebar
VBA Input & Output Functions
  • VBA Input & Output
    • VBA Login
    • VBA Outlook
    • VBA Print
    • VBA UserForm
    • VBA Close UserForm
    • VBA Send Email From Excel
    • VBA Debug Print

VBA Debug Print

By Ashwani JaiswalAshwani Jaiswal

VBA Debug Print

Excel VBA Debug Print

Debug Print is one of the most useful but underrated tools VBA has. Debug Print can be used in place of MsgBox. It helps in analyzing the process and output in the immediate window. Debug Print and MsgBox in VBA works on the same principles.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (85,982 ratings)

They both show the values like a message. But there are some major difference and benefits of using Debug Print over MsgBox. Debug Print, shows us the value stored in any variable or in itself in Debug Print like MsgBox. It also helps in debugging the error after executing the complete code. There is no need of clicking on the Ok button after we get the message, by which we can save a small amount of time and extra step to perform.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

There is no proper syntax of Debug Print. We can add anything and whatever we want to see as output in the immediate window.

How to Use Debug Print in Excel VBA?

Below are the different examples to use Debug Print in excel by using VBA code.

You can download this VBA Debug Print Excel Template here – VBA Debug Print Excel Template

Excel VBA Debug Print – Example #1

For applying Debug Print in Excel VBA, we need to follow the below steps.

Step 1: Go to the VBA window, under the Insert menu tab select Module as shown below.

VBA Debug Print Example 1-1

Step 2: In the newly opened Module, write the subcategory VBA Debug Print or you can choose any other name for that.

Code:

Sub VBA_Debug1()

End Sub

VBA Debug Print Example 1-2

Step 3: Now directly use Debug Print as shown below. As we discussed, Debug Print doesn’t have any syntax. We can choose anything we want to print.

Code:

Sub VBA_Debug1()

  Debug.Print

End Sub

VBA Debug Print Example 1-3

Step 4: Let’s print any random text which we want to see and quote that text in inverted commas to see the output.

Code:

Sub VBA_Debug1()

  Debug.Print "This is how Debug Print works!!"

End Sub

VBA Debug Print Example 1-4

Step 5: As we know, the output of Debug Print will only be seen in Immediate Window. So, we will be opening the Immediate Window from the View menu bar as shown below.

Immediate Window

After that, we will be getting a blank Immediate Window as shown below. This is a place where we will be seeing all the output from Debug Print.

Blank Immediate Window Example 1-6

Step 6: Now compile the code and run it by clicking on the Play button located below the menu bar. We will see, the Debug print has omitted the output in Immediate window as shown below.

VBA Debug Print Example 1-7

Excel VBA Debug Print – Example #2

In this example, we will see, how to print any value using Debug print in excel VBA. For this, follow the below steps:

Step 1: In a module, write the subcategory in any name as shown below.

Code:

Sub VBA_Debug2()

End Sub

VBA Debug Print Example 2-1

Step 2: We will need a variable as an Integer.

Code:

Sub VBA_Debug2()

  Dim A As Integer

End Sub

Declare Variable as Integer Example 2-2

Step 3: Assign any value to that integer.

Code:

Sub VBA_Debug2()

  Dim A As Integer
  A = 1

End Sub

VBA Debug Print Example 2-3

Step 4: Now we will use Debug print and assign the variable which we have just defined directly.

Code:

Sub VBA_Debug2()

  Dim A As Integer
  A = 1
  Debug.Print A

End Sub

VBA Debug Print Example 2-4

Step 5: Now we will run the code by pressing F5 key. We will see, the value stored in variable A is now printed in the Immediate window.

VBA Debug Print Example 2-5

In a similar way, let’s define different types of variables in the same code and see, what changes happen.

Step 6: Declare the 2 more variables as Double and Long where we will try to store decimal value and large values in 6 digits.

Code:

Sub VBA_Debug2()

  Dim A As Integer
  Dim B As Double
  Dim C As Long

End Sub

VBA Debug Print Example 2-6

Step 7: Now assign some values to each defined variable as per their character of data types. Assign the whole number to variable A, decimal value to variable B and any 6 digits or larger number to variable C.

Code:

Sub VBA_Debug2()

  Dim A As Integer
  Dim B As Double
  Dim C As Long

A = 1
B = 123.123
C = 123123123

End Sub

VBA Debug Print Example 2-7

Step 8: Now give individual Debug Prints to each of the variables so that, we will be seeing the separate values but in the same immediate window.

Code:

Sub VBA_Debug2()

  Dim A As Integer
  Dim B As Double
  Dim C As Long

A = 1
B = 123.123
C = 123123123

Debug.Print A
Debug.Print B
Debug.Print C

End Sub

VBA Debug Print Example 2-8

Step 9: Now run the complete code by pressing the F5 key or by clicking on the Play Button. We will see, in one shot all the values are stored in variables A, B, and C which can be seen in the immediate window.

VBA Debug Print Example 2-9

Excel VBA Debug Print – Example #3

In this example, we will see how any mathematical expression would run if we use an immediate window to see the output. For this, follow the below steps to use Debug Print in Excel VBA.

Step 1: Write the subcategory of VBA Debug Print as shown below.

Code:

Sub VBA_Debug3()

End Sub

VBA Debug Print Example 3-1

Step 2: Here, we will need to try to perform an addition of 2 variables. For this define 2 variables in which we will be submitting the input numbers and 3rd variable where we will store the output coming from addition of the first two variables. Let’s consider those variables as A, B, and C respectively.

Code:

Sub VBA_Debug3()

Dim A As Integer
Dim B As Integer
Dim C As Integer

End Sub

VBA Debug Print Example 3-2

Step 3: Now allot any numerical values to variable A and B. Here, we have considered those as 10 and 20 respectively.

Code:

Sub VBA_Debug3()

Dim A As Integer
Dim B As Integer
Dim C As Integer

A = 10
B = 20

End Sub

Numerical values Example 3-3

Step 4: For the purpose of adding, we will perform a mathematical function where we will add first and second variable A and B and get the output in variable C.

Code:

Sub VBA_Debug3()

Dim A As Integer
Dim B As Integer
Dim C As Integer

A = 10
B = 20
C = A + B

End Sub

Addition Function Example 3-4

Step 5: Now we will use Debug print to print the output of addition of variable A and B under variable C. So, only variable C will be assigned in Debug Print to see the output of addition.

Code:

Sub VBA_Debug3()

Dim A As Integer
Dim B As Integer
Dim C As Integer

A = 10
B = 20
C = A + B
Debug.Print C

End Sub

VBA Debug Print Example 3-5

Step 6: Now run the code by pressing F5 key or by clicking on Play Button. We will get the output of addition of values stored in variable A and B under C in the immediate window.

Addition of values Example 3-6

By this, we can perform any type of mathematical process and get the output in an immediate window instead of using MsgBox which is also easy but not as good as Debug Print.

Pros of Excel VBA Debug Print

  • It is easy to apply.
  • No need to change the window to see the output. That we easily can see in the immediate window.
  • We can even delete the output data from an immediate window once the purpose is incomplete.
  • Compiling the code is optional.
  • If we get any error then we can easily rectify that just from seeing the output in the immediate window.

Things to Remember

  • If there is an error in the code, then the immediate window will give us the output as 0.
  • If the text is long, then we can adjust the size of the immediate window as per our need.
  • We can also adjust the location of an immediate window.

Recommended Articles

This is a guide to VBA Debug Print. Here we discuss how to use Debug Print in Excel using VBA code along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA RGB
  2. VBA XML
  3. VBA Block Comment
  4. VBA IsError
Popular Course in this category
VBA Training (4 Courses, 12+ Projects)
  4 Online Courses |  13 Hands-on Projects |  50+ Hours |  Verifiable Certificate of Completion
4.7
Price

View Course
2 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more