This article will explain how to use the immediate window in the VBA debugging environment. This environment can be used as a kind of scratch area or sandbox and has many uses besides debugging code.

If you can’t see the Immediate Window, you just need to enable it. It’s really simple.

  • Open the Visual Basic Editor

  • View

  • immediate window

Or if you are crazy about shortcuts then you can do the following:

  • ALT+11 to open the Visual Basic Editor
  • CTRL+G to open the Immediate window and place the cursor in it

Are you ready. The window looks pretty bland and basic at first glance, but it’s really Really useful, for example, you can-

  • Get information about your current Excel workbook

We can ask questions in the Immediate window and wait for a response, we just need to use a question mark ‘?’. For example, typing in the window ?Worksheet.Account we are correct for example in my workbook it said there are 4 worksheets. Cool.

  • Run one line of VBA code and get immediate results

You can also just run a piece of VBA code from the Immediate window. Just remove the? from the beginning of your statement and the code will be executed. Let’s take a look at an example. We can make all hidden worksheets visible by writing this code in the Immediate window.

For each ws in sheets: ws.Visible = True:Next

This code snippet will iterate through all the worksheets and set their visible property to TRUE. Great job!. Hopefully this gives you an idea of ​​the power of the immediate window.

  • Run a macro from the immediate window

So, as stated at the top of this article, let’s run a macro from the Immediate window. It’s very simple again, we just need to type the name of the macro right there in the Immediate window.

Here is the macro again that will display all the worksheets in my workbook or make them visible (whatever you prefer to say it!).

Or named this macro Show_multiple_worksheets.

All I have to do is type the name of the macro in the Immediate window, hit return, and the macro will run. Do you want to try another?

Other ways to run a macro

  • Press F5

  • Press the Run Command button in the VB Editor in Excel.

Leave a Reply

Your email address will not be published. Required fields are marked *