Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to close all open workbooks except those of specific filename 1

Status
Not open for further replies.
Mar 6, 2003
157
JO
How do I create a macro to close all open workbooks except those of a specific filename(s). For example, I only want to keep Workbook1.xls and Workbook2a.xls open while I want to close every other excel file.

Any help would be greatly appreciated.

Thanks,
Shawn
 
Try the following code. Just replace "Book1" and "Book2" in the code with the actual names of the workbooks you want to keep open. The workbooks names ARE case sensitive so make sure to spell them exactly the way they are named without include the .xls extension.


Public Sub Workbooks()


Dim wb As Workbook
For Each wb In Application.Workbooks
If wb.Name <> &quot;Book1&quot; And wb.Name <> &quot;Book3&quot; Then
wb.Close
End If

Next wb



End Sub







Rollin Again
 
Shawn,

Consider using the Select Case structure
Code:
    For Each wb In Application.Workbooks
      With wb
        Select Case  .Name         
          Case &quot;Book1&quot;, &quot;Book3&quot;
            .Close
        End Select
      End with
    Next wb
:)

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
Skip, I intended to have my &quot;Book1&quot; & &quot;Book3&quot; files remaining open at the end of the subroutine. It appears that &quot;Book1&quot; & &quot;Book3&quot; are closed.

Rolling, your suggestion almost worked for me since &quot;Book1&quot; remains open, but &quot;Book3&quot; closes.


The end result of the subroutine should keep only &quot;Book1&quot; and &quot;Book3&quot; open while closing all other workbooks.


Thanks,
Shawn
 
Hi Skip,

I though the following change that I made would work, but I got an error:

For Each wb Not In Application.Workbooks
With wb
Select Case .Name
Case &quot;Book1&quot;, &quot;Book3&quot;
.Close
End Select
End with
Next wb


The &quot;Not In&quot; clause doesn't appear to be valid. It looked obvious to me.

Thanks,
Shawn
 
Code:
For Each wb Not In Application.Workbooks
      With wb
        Select Case  .Name         
          Case &quot;Book1&quot;, &quot;Book3&quot; 'leave open
          Case Else             'close
            .Close
        End Select
      End with
Next wb
:)

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
Hi Skip,

You final suggestion helped a great deal. Thanks very much for your help :)

Kind regards,
Shawn
 
I did not notice the NO IN structure
Code:
For Each wb In Application.Workbooks
      With wb
        Select Case  .Name         
          Case &quot;Book1&quot;, &quot;Book3&quot; 'leave open
          Case Else             'close
            .Close
        End Select
      End with
Next wb


Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884

Skip,
 
Hi,
Interesting topic... I tried this last code to close some specific Powerpoint presentations and it worked fine with ppt 97, but fails to work with ppt 2003 :S.
My program really needs to work in different PPT versions! Can you help me?


Dim aa As Presentation
For Each aa In Application.Presentations
With aa
Select Case .Name
Case &quot;presentation1&quot;, &quot;presentation2&quot;, &quot;presentation3&quot;
.Close
End Select
End With
Next aa


Huib
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top