ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   close all open workbooks except the active one (https://www.excelbanter.com/excel-programming/358075-close-all-open-workbooks-except-active-one.html)

Dav[_4_]

close all open workbooks except the active one
 

I am sure the answer is very simple, but at the start of a macro, I wish
to close all the workbooks that are not the activeworkbook (which
contains the macro). Can anyone tell me how.

Thanks in advance

Dav


--
Dav
------------------------------------------------------------------------
Dav's Profile: http://www.excelforum.com/member.php...o&userid=27107
View this thread: http://www.excelforum.com/showthread...hreadid=529987


Stefano Gatto

close all open workbooks except the active one
 
Hi Dav,

This worked on my PC:

Sub closeAllOtherWb()
Dim wkbWorkbook As Workbook

For Each wkbWorkbook In Workbooks
If wkbWorkbook.Name < ThisWorkbook.Name Then wkbWorkbook.Close
Next wkbWorkbook
End Sub
--
Stefano Gatto


"Dav" wrote:


I am sure the answer is very simple, but at the start of a macro, I wish
to close all the workbooks that are not the activeworkbook (which
contains the macro). Can anyone tell me how.

Thanks in advance

Dav


--
Dav
------------------------------------------------------------------------
Dav's Profile: http://www.excelforum.com/member.php...o&userid=27107
View this thread: http://www.excelforum.com/showthread...hreadid=529987



Dav[_5_]

close all open workbooks except the active one
 

Thanks for your help

That works excellently. I knew there would be a simple way, but it was
beyond me in finding it. The answer is always simple when you know
how!

Cheers

Dav


--
Dav
------------------------------------------------------------------------
Dav's Profile: http://www.excelforum.com/member.php...o&userid=27107
View this thread: http://www.excelforum.com/showthread...hreadid=529987


JMB

close all open workbooks except the active one
 
Just pointing out that if changes were made to any of the open workbooks
since the last save, Excel will prompt users to save changes (which comes
with a cancel button). It is possible not all workbooks will be closed.
You'll need to decide whether or not this is problem depending on who is
running the macro and what problems it could cause.


"Dav" wrote:


Thanks for your help

That works excellently. I knew there would be a simple way, but it was
beyond me in finding it. The answer is always simple when you know
how!

Cheers

Dav


--
Dav
------------------------------------------------------------------------
Dav's Profile: http://www.excelforum.com/member.php...o&userid=27107
View this thread: http://www.excelforum.com/showthread...hreadid=529987




All times are GMT +1. The time now is 06:27 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com