2019 VBA Clear Inmmediate window


Author Jeffrey Paesch

There are many post about clearing the immediate window. However none of them seems to work or at least work as I expected. For instance many of the pages suggest flush the immediate. To flush your immediate window in VBA you can use the following sub.

Sub VBE_immediate_flush()
    Debug.Print String(100, vbCr)
End Sub

When it comes to clear your window, the following statement  will help you:
Application.SendKeys "^g ^a {bs}".
However, the above statement has limitation, It can not be run while you are running your codes. Immediate windows can be cleared only in the reset (stop) mode.
Pasting the above statement at the beginning of your statements will not have a desirable result. Your immediate window will remain empty, although you unfocused the immediate window with "Doevents" or Application.SendKeys "{F7}" statement.
Therefore, the best solution to cleare the immediate window is with the following sub:

Sub VBE_immediate_clear()
    get_win = Application.VBE.ActiveWindow.Caption
    cmpnts1 = Mid(get_win, 1, InStr(get_win, "(") - 2)
    Application.SendKeys "^g ^a {bs}"
    Stop
    Application.SendKeys "{F7}"
    Set Application.VBE.ActiveCodePane = _
    ThisWorkbook.VBProject.VBComponents(cmpnts1).CodeModule.CodePane
End Sub

Cons
This sub enter the reset (stop) mode in order to give the immediate windows to clear.

pros
it clear the immediate windows for real
The sub can be call at the beginning of your statements.
The sub will later activate and focus the captured VBE window after the immediate cleaning.


Conclusion
I hope this code will save you lot of headache on research
 






No comments:

Post a Comment