Does anyone know how to clear the immediate window using VBA?
While I can always clear it myself manually, I am curious if there is a way to do this programmatically.
For cleaning Immediate window I use (VBA Excel 2016) next function:
Private Sub ClrImmediate()
With Application.VBE.Windows("Immediate")
.SetFocus
Application.SendKeys "^g", True
Application.SendKeys "^a", True
Application.SendKeys "{DEL}", True
End With
End Sub
But direct call of ClrImmediate()
like this:
Sub ShowCommandBarNames()
ClrImmediate
'-- DoEvents
Debug.Print "next..."
End Sub
works only if i put the breakpoint on Debug.Print
, otherwise the clearing will be done after execution of ShowCommandBarNames()
- NOT before Debug.Print.
Unfortunately, call of DoEvents()
did not help me... And no matter: TRUE or FALSE is set for SendKeys.
To solve this I use next couple of calls:
Sub ShowCommandBarNames()
'-- ClrImmediate
Debug.Print "next..."
End Sub
Sub start_ShowCommandBarNames()
ClrImmediate
Application.OnTime Now + TimeSerial(0, 0, 1), "ShowCommandBarNames"
End Sub
It seems to me that using Application.OnTime might be very useful in programming for VBA IDE. In this case it's can be used even TimeSerial(0, 0, 0).