Immediate Window

The Immediate window is extremely useful and allows you to test and execute small snippets of code.
This can be used for executing VBA statements directly, testing statements and helping to debug your code
Lets you run any VBA statement, manually while a program is running.
The immediate window is particularlay useful for testing variables or properties
The Immediate window can be dragged and positioned anywhere on your screen unless you have made it a dockable window from the Docking Tab of the Options dialog box.
You can close the window by clicking the Close box. If the Close box is not visible, double-click the Title bar to make the Close box visible, then click it.


(View > Immediate Window)
Shortcut key (Ctrl + G).



 


What can I use the Immediate Window For ?

  • Displaying messages

  • Execute commands immediately.

  • Evaluate expressions immediately.


This is a very useful way to pause code execution when the unexpected happens.
When a command is typed and Enter is pressed - the command is executed immediately.
To execute the command again click anywhere in the line and press Enter again.


You cannot enter block of code in the Immediate window since it only executes a line at a time after you press Enter.
If you are executing multiple statements in the Immediate window you do not have to declare your variables. Usign the "dim" keyword will generate an error.
You can use Debug.Print statements anywhere in your code to display variable values in the Immediate window. Very useful for seperating your code inot sections. Debug.Print "End of Section 3".
The Immediate window will not be oped by default if values are written to it, you should open it manually (View > Immediate Window) before execution starts.
There is currrently no way to clear the Immediate widnow programmatically.


The Local window does not allow you to change the values of your variables, just to display them.
It is even possible to change the contents of variables at run-time.
There are two ways to print to the immediate window, either by including Debug.Print statements in the actual code or by entering print statements directly.

Debug.Print Range("A1").Value 
? Range("A1").Value


Note In break mode, a statement in the Immediate window is executed in the context or scope that is displayed in the Procedure box. For example, if you type Print variablename, your output is the value of a local variable. This is the same as if the Print method had occurred in the procedure you were executing when the program halted.


Displaying Messages

use the immediate window to test a statement


Debug.Print
1) Displaying messages from your code using Debug.Print [condition]
If the condition returns False or zero, then break mode is entered on that line.
This statement allows you to enter break mode based on a condition.
Any Debug.Print messages that you have in your code will not affect the speed of execution of your code and can be left in when code is distributed.


Executing Commands

You can also execute commands or procedures directly.
Copy and paste the code from the Immediate window into the Code window but does not allow you to save code in the Immediate window.
Type or paste a line of code and press ENTER to run it.


Evaluate Expressions Immediately

To evaluate an expression in the Immediate Window, precede your expression with a question mark (?). This is a short-cut for "Print".
Print or ?

? Application.Caption 

This window can perform two main tasks:


? MsgBox("",vbOkCancel) returns 1 


Useful Information

Floating point numbers are generally displayed in the immediate window to at most eight places beyond the decimal point even when sixteen places exist.
The immediate window contains the last 200 outputs implemented by Debug.Print


Important

In the immediate window it is also possible to assign values to variables or properties or to start macros by giving their name.
In the immediate window you can introduce new variables without any previous declaration (even when Option Explicit is not declared)
You can execute lines of code directly into the Immediate window.
You can execute multiple lines of code (including loops).
You can include vbCrLf to make long text strings more readable.


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext