Stop searching. Start learning and developing your excel skills.
Macro
VBA
Formula
Function
Shortcut
Tricks

» » VBA To Close Workbooks

VBA To Close Workbooks

June 03, 2017 |
This is also related to below queries:
  1. excel vba close workbook without saving
  2. close active workbook vba
  3. save and close workbook vba
  4. excel vba save and close workbook without prompt
  5. close specific workbook vba
The Code:
Workbooks("Book1.xlsx").Close
'''' Close Book1.xlsx only
YourWorkbook = "C\Desktop\Book1.xlsx"
Workbooks(YourWorkbook).Close
'''' Close YourWorkbook which is named in the your module
ActiveWorkbook.Close SaveChanges:=False
'''' Close current workbook on display only. Set SaveChanges:=True if you need to save
Sub SaveAndCloseOpenWorkbooks()
    Dim wb As Workbook
 
    With Application
        .ScreenUpdating = False
     
        For Each wb In Workbooks
         
            With wb
                If Not wb.ReadOnly Then
                    .Save
                End If
                If .Name <> ThisWorkbook.Name Then
                    .Close
                End If
        End With

        Next wb
        .ScreenUpdating = True
        .Quit
    End With
End Sub
'''' Close all open workbooks

Note:
  1. Just repeat Workbooks("Book1.xlsx").Close and change Book1 with Book2, Book3, etc if you need to close multiple known workbooks at the same time
  2. If you want to close the workbook that you are working on, use ActiveWorkbook.Close
  3. You can also use ActiveWorkbook.Save to save the active workbook

No comments:

Post a Comment