Use VBA to Clear Immediate Window?

前端 未结 16 962
刺人心
刺人心 2021-01-30 00:19

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.

16条回答
  •  离开以前
    2021-01-30 00:41

    Here is a combination of ideas (tested with excel vba 2007) :

    ' * (this can replace your day to day calling to debug)

    Public Sub MyDebug(sPrintStr As String, Optional bClear As Boolean = False)
       If bClear = True Then
          Application.SendKeys "^g^{END}", True
    
          DoEvents '  !!! DoEvents is VERY IMPORTANT here !!!
    
          Debug.Print String(30, vbCrLf)
       End If
    
       Debug.Print sPrintStr
    End Sub
    

    I do not like deleting the Immediate content (fear of deleting the code by accident, so the above is a hack on some of the code you all wrote.

    This handles the problem Akos Groller writes about above: "Unfortunately, this only works if the caret position is at the end of the Immediate window"

    The code opens the Immediate window (or puts the focus on it), sends a CTRL+END, followed by a flood of newlines, so the previous debug content is not in sight.

    Please note, that DoEvents is crucial, otherwise the logic would fail (the caret position would not move in time to the end of the Immediate window).

提交回复
热议问题