Close All Excel Workbooks Except Active Workbook

Maryz

Board Regular
Joined
Dec 10, 2002
Messages
197
I’d like to place code in my Workbook_Open event that determines if there are any other Excel files opened in the current instance of Excel, and if so, closes them without saving leaving only the active workbook open. My end goal is to have only my file open in an instance of Excel by itself. I presume that I cannot develop code that opens up my workbook in a new instance of Excel from any other Excel file that may already be open. This would be ideal, but theoretically I cannot see how it can be accomplished as the workbook must already be opened before the Worbook_Open code can execute. Any help is much appreciated.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Maryz,

This code should do what you want :

Code:
Private Sub Workbook_Open()
Dim WB As Workbook, MyWB As Workbook

Set MyWB = ActiveWorkbook

For Each WB In Workbooks
If WB.Name <> MyWB.Name Then WB.Close
Next

Exit Sub


EDIT: This one may be better :

Code:
Private Sub Workbook_Open()
Dim WB As Workbook

For Each WB In Workbooks
     If Not (WB Is ActiveWorkbook) Then WB.Close
Next

End Sub
 
Upvote 2
20 years later..... it worked for me too! hahahhaha THANKS!!
All the other solutions I was finding were wayyyyyy too complicated. This helped me get around an issue where I had a VBA app open up, and hide its workbook, but anything inputted in the app would then communicate with a workbook that was already open so you would have to make sure all other workbooks were closed before opening this VBA app. This code made it so it just closes the other workbooks. THANK YOU!!
 
Upvote 1

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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