How to Clear the Immediate Window

Contents

Introduction

In a previous article titled “VBA print to console,” you saw the use of the immediate window in VBA. You learned from that article that the VBA immediate window is a kind of playground where the VBA programmer can test code to see whether it is responding as expected.

Well that then begs the question — how do you clear the immediate window? That’s the purpose of this article.

How to Clear the Immediate Window

You can clear (delete the content) of the immediate window manually or programmatically.

Clear Immediate Window Manually

Select the content that you want to delete and press on the delete button on your keyboard. It’s incredibly simple:

Deleting text in immediate window

Clear Immediate Window Programmatically

To clear the immediate window programmatically, you need to replicate what you are doing manually.

Let us identify all the sequences and keys involved when we do it manually.

The command to replicate key actions in VBA is Application.SendKeys

For example, if you want to programmatically press the A key, you should write the following code: Application.SendKeys “A”. For the control key, you would use the symbol “^”

Below is the list of keys than can be used and their corresponding codes.

KeyCode
BACKSPACE{BACKSPACE} or {BS}
BREACK{BREAK}
CAPS LOCK{CAPSLOCK}
CLEAR{CLEAR}
DELETE or DEL{DELETE} or {DEL}
DOWN ARROW{DOWN}
END{END}
ENTER (Numeric keypad){ENTER}
ENTER~ (tilde)
ESC{ESCAPE} or {ESC}
HELP{HELP}
HOME{HOME}
INS{INSERT}
LEFT ARROW{LEFT}
NUM LOCK{NUMLOCK}
PAGE DOWN{PGDN}
PAGE UP{PGUP}
RETURN{RETURN}
RIGHT ARROW{RIGHT}
SCROLL LOCK{SCROLLDOWN}
TAB{TAB}
UP ARROW{UP}
F1 Trough F15{F1} Trough {F15}

You can also use keys combined with Shift and/or Ctrl and/or Alt and/or Command. To do so, use the following table:

To combine keys withBegin the key code with
Shift+ (plus sign)
Ctrl^ (caret)
Alt% (percent sign)

Applying it to the sequences above, we should have the following:

1)  Activate the immediate window (Ctrl + g) code: Application.SendKeys "^g", True

2)  Select all the content (Ctrl + a): Application.SendKeys "^a", True

3)  Clear the content (Press the Delete key): Application.SendKeys "{DEL}", True

Now, let’s go to the lab. In the following exercise, you will write two procedures. The first one called LoadImmediateWindow will load the squares of integers from 1 to 10.

The second will programmatically clear the immediate window. To achieve that, follow these steps.

Step 1: Open the Visual Basic Editor and create a new module as seen in the gif below.

Create new module in VBE

Step 2: Write or Ccopy and paste the following code:

Sub LoadImmediateWindow()

Dim x As Integer
For x = 1 To 10
Debug.Print (x ^ 2)
Next x

End Sub
Sub ClearImmediateWindow()

Application.SendKeys "^g", True
Application.SendKeys "^a", True
Application.SendKeys "{DEL}", True

End Sub

Your screenshot should look like this:

Screenshot of code to delete text from the immediate window

You now have to run the macro (LoadImmediateWindow) to load the immediate window. After that, you will run the second macro (ClearImmediateWindow) to clear the immediate window. To run any macro, you need to put your cursor in the said macro and press on the Run Sub button or the F5 function key on your keyboard as shown below.

The code is run and the immediate window is cleared

Conclusion

You have just learned how to clear the immediate window manually and programmatically. As you can see, doing it manually is easier and faster than if you were to do it programmatically.

3 thoughts on “How to Clear the Immediate Window”

Leave a Reply

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