How to Use the VBA Immediate Window in Excel?
Last Updated :
16 Oct, 2022
The immediate window is similar to the console in Chrome, where we could execute the javascript. The immediate window allows us to execute macro code very fast and efficiently. Once, you learned it, you will always be found using it. Your immediate window can answer an infinite number of questions, like setting a cell value, changing the sheet of the name, counting the number of sheets, finding the active cell of the current worksheet, etc. In this article, we will learn different ways to use the immediate Windows in excel.
Opening Immediate Window
By default, the immediate window does not appear in the visual basic editor, but, could be activated by following these steps:
Step 1: Go to the developer tab, under the Code section. Click on Visual Basic(Alt + F11).
Step 2: VBA editor is opened. Click on View, in the menu bar. Select Immediate Window.
Step 3: The Immediate window(Ctrl + G), is opened in the VBA editor.
Different Ways to Use Immediate Window
There can be a huge number of cases where you could use immediate windows which can make your work faster and easy. An immediate window can run a macro, set variable values, debug, ask different questions using (?), and many more.
Using (?) Question Operator
There are multiple uses of the (?) question operator, like finding the active cell of the sheet, counting the number of sheets in a worksheet, etc. All the functions available in VBA can be used, in the immediate window. Some of the most used functions are explained below.
- Use of Comparison Operators
Comparison operators like <, >, = can be used in the immediate window, to check whether one variable is greater, less, or equal to another variable or not. For example, to check whether 5 is greater than 4, write ?5>4 in your immediate window, and the answer will be a boolean either true or false.
- Finding the Name of the Sheet
The name of any sheet can be found in a workbook, using Sheet{number}.Name function. For example, to find the name of the second sheet, the function used is Sheet2.Name.
Using the Immediate window, one can find the value of any cell in a worksheet, using Range(“Cell_Name”).Value. For example, 23 is the value of cell A1, so we can find the value of cell A1 by using the function Range(“a1”).Value.
- Count the Number of Sheets in a Workbook
The immediate window can be used to find the number of sheets in the workbook. This can be achieved by using function sheets.Count. For example, if we have only one sheet, then the output in the immediate window will be 1.
- Find the Value of Active Cell
Despite writing a big macro, to find the active cell value, you can simply run the function ActiveCell.Value in the immediate window and the value of that cell is printed. For example, 23 is the currently active cell value, type ActiveCell.Value in the immediate window, and 23 is printed in the window itself.
Changing the Name of a Sheet in the Workbook
The immediate window can be used to set a value, according to the function used. There is no need of writing a sub-procedure, for it. For example, you are given a worksheet name, “Sheet1”, and our task is to rename it “geeksforgeeks”. Following are the steps:
Step 1: Open your immediate window, in VBA Editor. You can see that name of the sheet is “Sheet1”.
Step 2: Use function, Sheet1.Name = “geeksforgeeks”, and set the name of the sheet as “geeksforgeeks”.
Step 3: Run your macro. You can observe that name of the sheet is changed to “geeksforgeeks”.
Set Cell Value
The range function is used to access a cell in the VBA, this function can also be used in the immediate window, and the value of the cell can be set accordingly. For example, if you want to set the value of cell A1, to 23, then we could use Range(“cell_name”).Value function. Following are the steps:
Step 1: Open your VBA macro, immediate window. Type Range(“A1”).Value = 23, function to assign the value to cell “A1”.
Step 2: Press Enter, to Run it. 23, appears in the cell, “A1”.
Debugging the Code
Debugging is a very important skill for every developer, and an immediate window makes it easy. The most important thing in debugging is to have print statements, in your code, so that you can know where your code breaks. VBA has a function, called Debug.Print(string/variable), which prints the required result in the immediate window itself, and there is no need of switching tabs and printing the content in the worksheet. For example, if you want to print 23 in your code, then use Debug.Print() function. Following are the steps:
Step 1: Open your VBA macro, and create a sub-procedure name geeksforgeeks().
Step 2: Type Debug.Print(23), in the sub-procedure, and run your macro.
Step 3: The number 23 is printed, in the immediate window.
Run Macro
There can be situations when your macro might not run from the run button. One of the possible situations is when you have arguments in the sub-procedure, then you cannot run your macro, using the run button. But, an immediate window resolves this issue. For example, write a sub-procedure that changes the active cell color to blue. The function used to achieve this is ActiveCell.Interior.Cell = color, where color is the argument of the sub-procedure colorChangeGeeks(color). Following are the steps:
Step 1: The given program changes the color of the active cell in the worksheet, the sub-procedure also takes an argument named “color”.
Step 2: In the immediate window call the sub-procedure, colorChangeGeeks(15123099), where 15123099 is the color code of blue.
Step 3: Press Enter, and you will observe that the color of the active cell is changed to blue.

Similar Reads
How to use While Wend Loop in Excel VBA?
In this article, we are going to see about While Wend loop in Excel VBA using a suitable example. Implementation : In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. The Developer Tab can be enabled easily by a two-step process : Right-click on
2 min read
How to Use Do While Loop in Excel VBA?
A Doâ¦While loop is used when we want to repeat certain set of statements as long as the condition is true. The condition may be checked at the starting or at the end of the loop Flowchart: Uses of Do-While loop: The Do While loop is used in two ways: Doâ¦while loop which checks the condition at the S
2 min read
How to use If-Else-If Statement in Excel VBA?
In this article, we are going to look into how to use the If Else If statement in Excel VBA using a suitable example. Implementation : In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. The Developer Tab can be enabled easily by a two-step proce
3 min read
How to use If-Else Statement in Excel VBA?
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to learn how to use the If Else statement in Excel VBA. Impl
2 min read
How to Use Select Case Statement in Excel VBA?
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to discuss how to use Select Case Statement in Excel VBA. Se
10 min read
How to Use For Next Loop in Excel VBA?
If you are familiar with the programming you must have an idea of what a loop is, In computer programming, a loop is a sequence of statements that are repeated until a specific condition is satisfied. In Excel VBA the "For Next" loop is used to go through a block of code a specific number of times.
4 min read
How to Find the Last Used Row and Column in Excel VBA?
We use Range.SpecialCells() method in the below VBA Code to find and return details of last used row, column, and cell in a worksheet. Sample Data: Syntax: expression.SpecialCells (Type, Value) Eg: To return the last used cell address in an activesheet. ActiveSheet.Range("A1").SpecialCells(xlCellTyp
2 min read
How to Use Solver in Excel?
A solver is a mathematical tool present in MS-Excel that is used to perform calculations by working under some constraints/conditions and then calculates the solution for the problem. It works on the objective cell by changing the variable cells any by using sum constraints. Solver is present in MS-
3 min read
How to Use the VBA Editor in Excel: Quick Guide 2024
Unlock the full potential of Microsoft Excel by diving into the world of Visual Basic for Applications (VBA). The VBA Editor in Excel is a powerful tool that allows you to automate tasks, create custom functions, and streamline your workflow like never before. Whether you're looking to boost product
7 min read
How to use Do Until Loop in Excel VBA?
In this article, we are going to see look into the Do Until loop in Excel VBA using a suitable example. Implementation : In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. The Developer Tab can be enabled easily by a two-step process : Right-cli
3 min read