In this post, you’ll learn how to use the Immediate window in your Excel spreadsheet when working with Excel VBA.
What is immediate window in Excel VBA?
The immediate Window tool in VBA is used to allow you to test individual lines of code and debug them. It quickly runs each individual code and gets information about Excel files. And the output is displayed in the Immediate Window.
Displaying the Immediate Window
To display the Immediate Window first:
- Press Alt-F11 to enter the Visual Basic Editor or under the Developer tab in the ribbon click on the Visual Basics option.
- No click on the insert option and click on the module option.
- Press Ctrl-G to view the Window.
Executing Lines of Code
In the immediate window enter the following code:
Range("A1").Interior.Color = RGB(245, 245, 245)
It is not required to have a header and footer for the code as it gets executed right away.
Questions and the Immediate Window
You can use the immediate window to get information about your workbook. Let’s say the active cell has a value of 57 and you want to print them. But you are unsure of the command. The type Question mark ‘?’ before the code.
?ActiveCell.Value
Run a Macro from the Immediate Window
You can run a macro from the Immediate Window by typing the name of the macro and pressing Enter. You can also pass arguments to the macro through the Immediate Window.
Let’s create a macro named CallaMacro. This macro contains a code, which will change the BG of the cell D5.
CallaMacro
Press enter and you can see the result in D5.
Using Debug.Print
Debug.Print  tells the Immediate Window to display certain information. Debug.Print statemnet is used to avoid display values or results in a cell in the workbook ior in a message box.
Code:
Sub UsingDebugPrint() Dim WebsiteName As String Dim SecondHalf As String WebsiteName = "Developer" SecondHalf = "Publish" Debug.Print WebsiteName & " " & SecondHalf End Sub
Using the Immediate Window When Running Code
You can also use the Immediate Window to set or get a variable’s value while you are stepping through your code:
Sub UsingDebugPrint() Dim WebsiteName As String Dim SecondHalf As String Dim Stream As String WebsiteName = "Developer" SecondHalf = "Publish" Stream = "Software and IT tutorials" End Sub
Insert a breakpoint in the code and now when you type the variable used in your code in the immediate while stepping into your code using F8, the value of the variable is returned.