• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User


  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Immediate Window, Locals Window, Watch Window


Excel Ninja

Can experts on the forum please guide us (if possible) regarding how to us below mentioned features in VBA.

Immediate Window, Locals Window and Watch Window

I use Immediate Window but I am not aware of all the features of it.
Also, have never used Locals Window and Watch Window.

Thanks in advance. :)
Hi Throttle. Good question! I'll try to do the best I can.

Immediate Window
The immediate window has three purposes. The first is for program output. You can use the:
command to send something to the Immediate Window. This can be helpful when debugging some code to see what a variable's value is at a certain point, or to provide any other useful information.
The other half is to provide immediate input. Let's say your code is running along and stops for some reason. Perhaps a variable has the wrong value and it's causing an error. You could stop, correct the code, and restart, but sometimes that takes too long or we can't easily start over. You can change type any line of code into the Immediate Window and upon hitting Enter, the code will execute. This could be as simple as selecting a Range, changing a variables value, or anything else where you want to have the code immediately do something. Benefit here is that doing stuff in Immediate window doesn't affect the code that is currently paused.
The last purpose is to query something. If you put a question mark before a line of code, the window will try to return the result. This could be used to get the value of a variable in code that is currently running, or if you wanted to know the value of a cell, etc.
Examples of lines in immediate window

Locals Window
The locals window lets you see all variables that are being used in the current sub that is running. It displays their name, value, and what type they are. Example image:

Note that x is the only variable that has been defined. y and z therefore are define as a Variant, but we can see that they currently are holding an integer and a long. The variable values from sub "bob" are not shown because they are not in the current sub.

Watch Window
Let's say you have some variable that you want to watch what it's value is. Either you can watch it all the time, or have the code stop when it reaches a set value. The Watch window will show all the variables that you have added a watch to (not just local ones).

In the screen cap, i've added two watches to the Y variable. The first one with the exclamation is a stop watch. I set one up so that after y changes, code stops (hence why it's currently highlighting the line after "y = 2". The 2nd line in watch window is a regular watch, showing the glasses symbol.

That's my brief overview of each. Follow-up questions?