Code Samples for Businesses, Schools & Developers

Last Updated 18 Apr 2017

I use the following function to close all VBE windows
Its very useful when so many windows are left open you can't find anything.
It also significantly speeds up loading the VBE editor if it doesn't need to load lots of unwanted 'legacy' windows

CODE:

Function CloseAllVBEWindows()
'closes all VBE windows except this one!
'requires VBA reference library 'Microsoft Visual Basic for Applications Extensibility'

On Error GoTo Err_Handler

Dim vbWin As VBIDE.Window
For Each vbWin In Application.VBE.Windows
    If (vbWin.Type = vbext_wt_CodeWindow Or _
        vbWin.Type = vbext_wt_Designer) And _
        Not vbWin Is Application.VBE.ActiveWindow
                vbWin.Close
    End If
Next

Exit_Handler:
   Exit Function

Err_Handler:
   If Err.Number = 424 Then Resume Next     'object required
   MsgBox "Error " & Err.Number & " in CloseAllVBEWindows procedure: " & Err.Description
   Resume Exit_Handler

End Function


To use the above code, just copy it to a standard module & run it

NOTE:
1. As stated in the code, add a reference to the library 'Microsoft Visual Basic for Applications Extensibility'

2. After running the function, all windows will be closed except the one containing the function itself.
   Alternatively, to close all ALL windows, create an Autokeys macro shortcut for this function e.g. Ctl+Shift+X

3. A small modification can be made to the above code so the additional reference isn't required.
   This change was suggested by Access World Forums member CJ_London

CODE:

Function CloseAllVBEWindows()

'closes all VBE windows except this one!

On Error GoTo Err_Handler

'modified section
Dim vbWin As Object
Const vbext_wt_CodeWindow = 0
Const vbext_wt_Designer = 1

For Each vbWin In Application.VBE.Windows
    If (vbWin.Type = vbext_wt_CodeWindow Or _
        vbWin.Type = vbext_wt_Designer) And _
        Not vbWin Is Application.VBE.ActiveWindow Then
                vbWin.Close
    End If
Next

Exit_Handler:
   Exit Function

Err_Handler:
   If Err.Number = 424 Then Resume Next     'object required
   MsgBox "Error " & Err.Number & " in CloseAllVBEWindows procedure: " & Err.Description
   Resume Exit_Handler

End Function




Return to Code Samples Page Return to Top