logo ASAP Utilities

Excel tip: Close multiple files at once

Date: 5 september 2001

When you work with a lot of files, a code to close all files might become handy.

Copy-paste friendly code:
Sub CloseSavedFiles()
' closes all saved files
      Dim wkbOpen                            As Workbook

      If MsgBox("Do you wish to close ALL saved workbooks?", vbQuestion + vbOKCancel, _
                "Close all saved workbooks?") = vbCancel Then
            Exit Sub
      End If

      Application.ScreenUpdating = False
      For Each wkbOpen In Application.Workbooks
            If wkbOpen.Saved Then wkbOpen.Close savechanges:=False
      Next
      
End Sub

Sub CloseAllFiles()
' closes ALL files
' be carefull, this will also close files that might not have been saved
      Dim wkbOpen                            As Workbook

      If MsgBox("Do you wish to close ALL workbooks," & vbNewLine & _
                "even if they have not been saved ?", vbQuestion + vbOKCancel, _
                "Close all workbooks?") = vbCancel Then
            Exit Sub
      End If

      Application.ScreenUpdating = False

      For Each wkbOpen In Application.Workbooks
            wkbOpen.Close savechanges:=False
      Next wkbOpen

End Sub



« back
Share this page:
Home Sitemap Search List of all utilities Printer friendly page ^ Back to Top
© 1999-2017 - - - - The Netherlands

Connect with us and receive our tips that show you how to benefit from the tools in ASAP Utilities at:
Follow us on TwitterFollow us on Facebook