ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Close all other workbooks but this one? (https://www.excelbanter.com/excel-programming/308740-close-all-other-workbooks-but-one.html)

ianripping[_93_]

Close all other workbooks but this one?
 
If I have 3 workbooks open and I only want to have open (this) workbook
is there a way to make excel find the number of open worksheets an
close them all apart from this open one

--
Message posted from http://www.ExcelForum.com


Harald Staff

Close all other workbooks but this one?
 
Sub test()
Dim WB As Workbook
For Each WB In Application.Workbooks
If WB.FullName < Me.FullName Then WB.Close
Next
End Sub

As is it will ask "save changes ?" for each one.

HTH. Best wishes Harald

"ianripping " skrev i melding
...
If I have 3 workbooks open and I only want to have open (this) workbook,
is there a way to make excel find the number of open worksheets and
close them all apart from this open one?


---
Message posted from http://www.ExcelForum.com/




Nikos Yannacopoulos[_5_]

Close all other workbooks but this one?
 
Sub close_all_other_workbooks()
ThisWkbk = ActiveWorkbook.Name
For i = Workbooks.Count To 1 Step -1
If Workbooks(i).Name < ThisWkbk And Workbooks(i).Name < "Personal.xls"
Then
Workbooks(i).Close 'False
End If
Next
End Sub

Uncommenting the False switch will close without saving, using True instead
will save and close; as is, you will be prompted.

HTH,
Nikos

"ianripping " wrote in message
...
If I have 3 workbooks open and I only want to have open (this) workbook,
is there a way to make excel find the number of open worksheets and
close them all apart from this open one?


---
Message posted from http://www.ExcelForum.com/




ianripping[_94_]

Close all other workbooks but this one?
 
Thanks, works great

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 03:29 PM.

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