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
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”
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.
#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:
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.
#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.
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