Clearing VBA Immeidate Window

Mackeral

Board Regular
Joined
Mar 7, 2015
Messages
248
Office Version
  1. 365
Platform
  1. Windows
This a test for the standard way of testing clearing the Debug window:
VBA Code:
Function Clear_Immediate()

     For X = 1 To 10
        Debug.Print X
    Next    
    Debug.Print Now

    Application.SendKeys "^g ^a {DEL}"
    
    Debug.Print "Test"
  
End Function
When I run it,
VBA Code:
"Debug.Print "Test"
doesn't print out anything.

I have found lots of code that says it can do it, but I can't get it to work. Has something changed in VBA about how to do this.

My point here is clear and then print in the Immediate Page
VBA Code:
---------1---------2---------3---------4---------5---------6---------7---------8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
to check formatting with, and it would really be nice to get the auto clear to work.

Thanks for any help. Mac
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
When I run this the "Test" does indeed print, but is immediately cleared as the Sub finished (I wrote it as a Sub because there is no return value). However, when I step through it with the debugger, it is not cleared. I suspect it has something to do with how SendKeys queues and send characters. I suspect the commands are somehow sent more than once.
 
Upvote 0
Sendkeys is not reliable but, you can always run the subsequent code section after a brief delay:
VBA Code:
Sub Clear_Immediate()

     For X = 1 To 10
        Debug.Print X
    Next
    Debug.Print Now

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

   Application.OnTime Now, "DBGPRINT"
  
End Sub

Sub DBGPRINT()
    Debug.Print "Test"
End Sub
 
Upvote 0
Jafar,
I added a <Debug.Print "Bye"> just before the end of "Clear_Immediate" and notice that the "Bye" never prints
which kills Debug.Print.

After I do the "Clear_Immediate", I want to print something at the top of the Immediate window, and
this doesn't let this happen.

I put a 1 minute "Wait" in my code which does clear the Immediate Window, but then I get an error message saying
"Can't execute code in Break Mode" and I don't know where that's coming from.

I really appreicate you help here. Mac
 
Upvote 0
Here is the code I'm trying to make work.
VBA Code:
Sub Debug_Clear()
    ' Clear the Immediate Window.
    ' 3/16/19 From StackOverflow "Use VBA to Clear Immediate Window?" WML

    If True Then     
    
        For X = 1 To 10
            Debug.Print X
        Next
        Debug.Print Now
    
        Application.SendKeys "^g ^a {DEL}"
    
        Application.OnTime Now, "DBGPRINT"
        Debug.Print "hI"

        Line1 = ""
        Line2 = ""
        
        For i = 1 To 8
            Call Add_Seperator(Line1, "", "---------" & i)
            Call Add_Seperator(Line2, "", "1234567890")
        Next i
        
        Debug.Print Line1
        Debug.Print Line2
    
    End If
    
End Sub ' Debug_Clear
 
Upvote 0
@Mackeral, I spent a few hours on this today to see if I could make some progress on this situation.

The following is the best code I came up with to achieve your intended goal, it's not what I would like, but is the best I could come up with:

VBA Code:
Sub ClearAndPrintToImmediateWindow()
'   Goal = Use VBA code to clear the contents of the 'Immediate' Window in the Developer/Visual Basic section of Excel and then
'       be able to send Debug.Print commands to the 'Immediate' Window after the contents of the 'Immediate' Window have been cleared.
'
    For X = 1 To 10                                                                 ' Print out 10 lines of test data ( 1 - 10 ) into the 'Immediate' Window
        Debug.Print X                                                               '   Print out vale of 'X'
    Next                                                                            ' Loop back to increment value of 'X'
'
    Call ClearImmediateWindow                                                       ' Call the Subroutine that clears the 'Immediate' Window
'
    Application.OnTime Now + TimeSerial(0, 0, 0), "FirstDebugPrintAterClearing"     ' Call the Subroutine that prints out the first line after the clearing occurred
'
    Application.OnTime Now + TimeSerial(0, 0, 1), "SecondDebugPrintAterClearing"    ' Call the Subroutine that prints out the second line after the clearing occurred
'
    Application.OnTime Now + TimeSerial(0, 0, 2), "ThirdDebugPrintAterClearing"     ' Call the Subroutine that prints out the third line after the clearing occurred
End Sub
'
'
' To use .VBE here, you have to enable VBA project access via:
'                                                   Excel Options/Trust Center/Trust Center Settings/Macro Settings/Trust Access to the VBA project object model
Sub ClearImmediateWindow()
    With Application.VBE.Windows("Immediate")
        Application.SendKeys "^g ^a {DEL}"
    End With
End Sub
'
Sub FirstDebugPrintAterClearing()
    Debug.Print "Immediate Window has been cleared ... Awaiting Further Debug.Print"
End Sub
'
Sub SecondDebugPrintAterClearing()
    Debug.Print "2nd Debug.Print after Immediate Window was cleared"
End Sub
'
Sub ThirdDebugPrintAterClearing()
    Debug.Print "3rd Debug.Print after Immediate Window was cleared"
End Sub

Not sure if that will help you on your path, but it does print code to the 'Immediate' window, clears it, then writes 3 additional strings to the 'Immediate' Window.

Not sure why you can't just manually clear the window, but hey, this is what I came up with for your goal, as I understand it to be.

Hopefully it assists you, and may inspire other ideas from you or some of the smarter members here.
 
Last edited:
Upvote 0
Solution
Thanks for thinking this through for me.
I am a code writer and am always looking for ways to show debugging information.
 
Upvote 0
Continuing with a question:
I've tried clearing and waiting for 30 seconds and then trying to print something. That doesn't work.
But if I leave the subroutine and then start Debug.Printing, I can print sequential lines with no problem.
What does the time have to do with it?
 
Upvote 0
The following is the best code I came up with to achieve your intended goal, it's not what I would like, but is the best I could come up with:

VBA Code:
' To use .VBE here, you have to enable VBA project access via:
' Excel Options/Trust Center/Trust Center Settings/Macro Settings/Trust Access to the VBA project object model
Sub ClearImmediateWindow()
    With Application.VBE.Windows("Immediate")
        Application.SendKeys "^g ^a {DEL}"
    End With
End Sub

I pasted this code into my add-in module. The Sendkeys statement caused the Sub to execute without any error messages. The window was not cleared.

I use many different functions in my add-in module, so I know it is accessible.

I then noticed the note about the Trust center. I discovered that that option was unchecked. I checked it and tried again. The Sub executes and exits normally, but the window Is not cleared.

Is there something else I need to do?

I realize that this is an old thread, but it was all I could find about clearing the Immediate Window.
 
Upvote 0

Forum statistics

Threads
1,221,544
Messages
6,160,428
Members
451,645
Latest member
androidmj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top