What is Immediate Window and how to use it in Excel VBA

.

Dear Friends,

Have you heard of Immediate Window in Excel VBA programming? Do you know how to make use of it?

If you have not been using this while doing vba programming then trust me you were missing something really cool stuff which excel provided to the programmers.

In this article I am going to explain you the following

1. What is immediate window in Excel VBA and how to open it?
2. What is the use of Immediate windows?
3. Top 5 Usage of immediate window with examples

What is immediate window in excel vba

Immediate window is a plane white background simple window in Visual Basic Editor, which can be used for different purposes. By Default, this window is not visible when you open the VBE in your excel. To view this window in your Excel VBE Click on View –> Immediate Window ( or Use Shortcut key Ctrl + G) to view this window as shown in below picture

Excel VBA - View Immediate Window

Excel VBA – View Immediate Window

This simple looking window is not as simple as it looks. It is powerful.
As the name of this window itself suggests… this helps in doing many thing while VBA programming very fast (immediately). That could be the main reason why this window was named as “Immediate Window”

Excel VBA - Immediate-Window

Excel VBA – Immediate-Window

Usage of this immediate window

#1 – To display the result of Debug.print xyz statement in VBA

This is the screen where you can see the result of statement Debug.print xyz statement which is executed while running your VBA program.
Debug.print variable, data etc. is the statement which is used to print value of any variable, object etc.

Immediate Window - DebugPrint

Immediate Window – DebugPrint

#2 – Simplest way to execute any Excel VBA statement

Immediate window can be used to execute excel vba statements directly here. You do not need to write the complete function structure to execute.
Example:To get the count of all sheets in a Workbook, you need to write the complete function in VBE like this



Sub getSheetCount()

MsgBox Worksheets.Count

End Sub


Now when you execute this function then you get the sheets count in a message box.

Using immediate window, you can execute this statement Worksheets.Count directly and get the count in immediate window itself like shown in below image:

Execute VBA Statement in Immediate Window

Execute VBA Statement in Immediate Window

Similarly you can execute any statement here in this window.

#3 – You can call any Macro/Function from this immediate window

If you want to test a function, especially which expects some input parameters, you can call them by using this immediate window.
Example: You have written a simple business function to calculate factorial of given number (input parameter). To test this function, you can not run it without passing the input parameter to this function. To do this, you can simply call this function from immediate window by providing the input parameter.

Function Call in Immediate Window

Function Call in Immediate Window

#4 – Set or get value of a variable during Debug mode

This window can be used to set or get any variable value while running your macro in a debug mode.
While running your VBA code in debug mode, if you want to test your function for a particular case – When value of variable ABC= XYZ (where XYZ – is a variable which is used in your function)

In such case, you can set a particular value to a particular variable of you function while running it a debug mode.

Useful Usecase of this feature

For example, if you have a loop in your code and you want to debug your loop for the 6th iteration. In such case, if you run your code in debug mode and put your break point on the beginning of the loop then you have to run the whole loop 6th time and then you will be able to debug it for 6th iteration.
But by using immediate window, you can set the counter variable to 7 directly and your loop will run for 6th iteration itself.

Set and Get Variable Values

Set and Get Variable Values

5# Few important points to remember

1# To get value of any variable or statement – start with question mark sing (?)
2# To assign any value to a variable or statement equal sign can be used
3# Immediate window can be undocked from VBE screen as shown in below image

Undocked Immediate Window

Undocked Immediate Window

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

0 Comments

Trackbacks/Pingbacks

  1. 5 rarely used feature in Excel VBA - Welcome to LearnExcelMacro.com - […] more about this, I have written a detailed article about this. Read this article to know more about:What is…

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest