PDA

View Full Version : Close multiple workbooks



catharsis50
03-07-2012, 02:24 PM
This isn't working for me:

workbooks("name of workbook.xlsm","name of workook2.xlsm").Close True


What ways are you able to specify which works books are to save and close at then end of a macro?

Thanks!

marreco
03-07-2012, 02:48 PM
Try.
Public Sub CloseAllWorkbooks()

Dim wb As Workbook



For Each wb In Workbooks

wb.Close False ' Or True if you want changes saved

Next wb



End Sub

Or
Autor:Justinlabenne
Option Explicit

Sub CloseAndSaveOpenWorkbooks()
Dim Wkb As Workbook

With Application
.ScreenUpdating = False

' Loop through the workbooks collection
For Each Wkb In Workbooks

With Wkb

' if the book is read-only
' don't save but close
If Not Wkb.ReadOnly Then

.Save

End If

' We save this workbook, but we don't close it
' because we will quit Excel at the end,
' Closing here leaves the app running, but no books
If .Name <> ThisWorkbook.Name Then

.Close

End If

End With

Next Wkb


.ScreenUpdating = True
.Quit 'Quit Excel
End With
End Sub

Paul_Hossler
03-07-2012, 06:44 PM
workbooks("name of workbook.xlsm").Close True
workbooks("name of workbook2.xlsm").Close True


Paul

catharsis50
03-08-2012, 03:07 PM
Thanks!