Excel VBA-How to close all open workbooks except one specific?

W

waveracerr

I have a macro that will close all open workbooks, works great. Now I
would like to have this macro close all open workbooks except one
specific files, KeyWest.xls. The below code works and I imagine a
simple IF Then sort of statement might solve my problem but I cannot
figure it out. Thanks for any help!

Ryan

Public Sub CloseAll()

Dim WB As Workbook

SaveAll
For Each WB In Workbooks
If Not WB.Name = ThisWorkbook.Name Then
WB.Close SaveChanges:=True
End If
Next WB

ThisWorkbook.Close SaveChanges:=True
End Sub
 
B

Bob Phillips

Hi Ryan,

It currently closes all workbooks except Thisworkbook (the workbook
containing this code). Do you just want to change from This workbook to Key
west.xls? If so, be aware that if Thisworkbook is not Key West, you will
close ThisWorkbook, at which point the macro cannot run any longer.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
T

Tomek

If You've placed this macro in KeyWest.xls, what suggests the line:
If Not WB.Name = ThisWorkbook.Name Then then remove the line:
ThisWorkbook.Close SaveChanges:=True
otherwise change the first mentioned line to:
If Not WB.Name = "KeyWest.xls" Then

Tomek
 
W

waveracerr

Ah great, I somehow missed the code:

ThisWorkbook.Close SaveChanges:=True


Teach me not to just copy and paste....

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top