Sub CloseCodePanes()
Dim i As Integer
On Error Resume Next
For i = Application.VBE.CodePanes.Count To 1 Step -1
Application.VBE.CodePanes(i).Window.Close
Next i
End Sub
It's quite erratic and sometimes closes everything and other times leaves one pane/window open. Anyways, I have two questions based on this code:
1. Is it possible to modify it to close all panes, except the pane that contains this macro?
2. Is it possible to have this macro trigger when the VBE opens (like an auto_open macro)?
TX
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Sub CloseAllVBEWindows()
Dim i As Long
With Application.VBE.Windows
For i = .Count To 1 Step -1
With .Item(i)
If .Type = 0 Then ' 0 = vbext_wt_CodeWindow
.Close
End If
End With
Next i
End With
End Sub
By the way, the free add-in MZTools adds a menu option to close all VBA windows (optionally keeping the active window open).
2) I'm not sure it would be desirable to close all code windows each and every time the Visual Basic Editor is opened. You could create a separate macro to open the VBE and close all code windows:
I am the original poster of Close All code windows/panes in the VB Editor
Thanks for linking me here Rudi, as per I was going to link the source query back to here if I got a reply...(which I will be doing now)
This has been very helpful and informative. I'll probably put the macro in my personal WB with a keyboard shortcut.
It turns out that the window that was being left open was a "form" design window so that probably explains that problem in my case.
I've done some hunting but can't find a list of the windows "types" so I can add the "form" window for closing as well.
Again, appreciate the help
First time to this forum and will definitely be back.
Brian
Make sure that you set a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 library in Tools > References in the Visual Basic Editor.
Press F2 to activate the Object Browser.
Type vbext_WindowType in the search box and press Enter.
You'll see a list of the possible window types:
S0547.png
A userform is probably of type vbext_wt_Designer = 1.
You do not have the required permissions to view the files attached to this post.
Sub subccCloseCodePanes()
' PROJECT.
' NO REPORT.
' NO END MESSAGE.
'
Dim olPane As Object
Dim vbcmlCodeModule As VBIDE.CodeModule
Dim vbclComponent As VBIDE.VBComponent
Dim vbplProject As VBIDE.VBProject
Dim ilPCount As Integer
Do
ilPCount = Application.VBE.CodePanes.Count
On Error Resume Next
Application.VBE.CodePanes.Item(ilPCount).Window.Close
If Err.Number <> 0 Then
Exit Do
End If
Loop
' ***********************************************************************
End Sub
Sub subccCloseOtherCodePanes()
' PROJECT.
' NO REPORT.
' NO END MESSAGE.
'
Dim ilPCount As Integer
Dim slOpenWindowCaption As String
slOpenWindowCaption = Application.VBE.ActiveCodePane.Window.Caption
Do
ilPCount = Application.VBE.CodePanes.Count
If ilPCount = 1 Then
Exit Do
End If
If Application.VBE.CodePanes.Item(ilPCount).Window.Caption _
<> slOpenWindowCaption Then
On Error Resume Next
Application.VBE.CodePanes.Item(ilPCount).Window.Close
If Err.Number <> 0 Then
Exit Do
End If
End If
Loop
On Error GoTo 0
' ***********************************************************************
End Sub
this must be 3rd or 4rth revision.. closes all code panes and designer windows from all unprotected open workbooks and addins
Sub CloseCodepanes()
Dim Module As VBComponent
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
For Each Module In wb.VBProject.VBComponents
If Module.Type = vbext_ct_MSForm Then
Module.DesignerWindow.Visible = False
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.DesignerWindow.Visible
Else
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.CodeModule.CodePane.Window.Visible
End If
Next
Next
Dim ad As AddIn
For Each ad In AddIns
Debug.Print ad.Name
If WorkbookIsOpen(ad.Name) = True Then
If ProtectedVBProject(Workbooks(ad.Name)) = False Then
For Each Module In Workbooks(ad.Name).VBProject.VBComponents
If Module.Type = vbext_ct_MSForm Then
Module.DesignerWindow.Visible = False
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.DesignerWindow.Visible
Else
Module.CodeModule.CodePane.Window.Visible = False
Debug.Print Module.Name, Module.CodeModule.CodePane.Window.Visible
End If
Next
End If
End If
Next
Debug.Print "All codepaned hidden"
End Sub
Function ProtectedVBProject(ByVal wb As Workbook) As Boolean
If wb.VBProject.Protection = 1 Then
ProtectedVBProject = True
Else
ProtectedVBProject = False
End If
End Function
Function WorkbookIsOpen(ByVal sWbkName As String) As Boolean
WorkbookIsOpen = False
On Error Resume Next
WorkbookIsOpen = Len(Workbooks(sWbkName).Name) <> 0
On Error GoTo 0
End Function
Last edited by AlexOfRhodes on 31 Mar 2022, 06:01, edited 2 times in total.
The original question was about closing all code panes. The code posted above does close all code module windows, including standard modules, class modules and userform modules, but not the userforms themselves.
The code that you posted - thanks! - closes the userform windows too.
Sub CloseAllVBEWindows()
Dim i As Long
With Application.VBE.Windows
For i = .Count To 1 Step -1
With .Item(i)
If .Type <= 1 Then ' 0 = vbext_wt_CodeWindow, 1 = vbext_wt_Designer
.Close
End If
End With
Next i
End With
End Sub
I see what you mean. Because i always used my codepanes in fullwindow i hadn't realized till now that the userform designer and userform codepane were separate that way.
The original question was about closing all code panes. The code posted above does close all code module windows, including standard modules, class modules and userform modules, but not the userforms themselves.
The code that you posted - thanks! - closes the userform windows too.
Sub CloseAllVBEWindows()
Dim i As Long
With Application.VBE.Windows
For i = .Count To 1 Step -1
With .Item(i)
If .Type <= 1 Then ' 0 = vbext_wt_CodeWindow, 1 = vbext_wt_Designer
.Close
End If
End With
Next i
End With
End Sub