An Elegant Way to Clear the Excel VBA Immediate Window When Debugging

Your Immediate Window is so jammed with debug.print that you just want to clean slate it... quickly... and elegantly... like a gazelle.

me@jaykilleen.com wrote this over 5 years ago and it was last updated over 5 years ago.


← Back to the Posts

I've wrestled this one a few times and Googled, StackOverflowed and had a few cracks at rolling my own.

I think I have finally got an elegant solution that reduces unpredictability and makes it fast to just clear the immediate window.

I use this mainly when I am debugging and just need to get rid of all the junk punched out by debug.print.

Either way... here ya go!

I have added this to my VBA Functions Boilerplate. I am also playing with a bashrc type of module where I can have aliases or shortcuts to use in my immediate window. Things like running my main macro, setting debug mode on, applying breakpoints to code using stop etc etc... I'll add the result of this later once I am happy... in the meantime, you will see me referencing this in the Bashrc.Clear step.

Function Clear()
  Application.VBE.Windows("Immediate").SetFocus
  If Application.VBE.ActiveWindow.Caption = "Immediate" And Application.VBE.ActiveWindow.Visible Then
    Application.SendKeys "^a {DEL} {HOME}"
  End If
End Function

Function C()
  Call Bashrc.Clear
End Function

To use this Clear or C (bashrc type aliases shortcut) just click in the Immediate Window and type 'C' the Enter.

I only do this when debugging so I am not sure how it will go with adding it to some type of runtime function or procedure. Let me know if it works for you or breaks your Excel.