To close an Excel workbook, you may use keyboard shortcuts for a fast approach or may use the Excel File tab. But for some specific purposes, you may need to use the VBA. This article will show you all the available ways to close the Excel workbooks.
Use Keyboard Shortcut to Close Excel Workbook
- Press Alt + F4 to close an Excel workbook. By applying this shortcut your Excel window will also disappear.
- Here is another shortcut available to close an Excel workbook. Press Ctrl + W or Ctrl + F4.
- The difference of this shortcut from the previous one is that now this time your workbook will close but the Excel window will not disappear.
Use File Tab to Close Excel Workbook
If you are not a big fan of using keyboard shortcuts, you can do it easily by clicking the mouse.
- Go to File >> Close.
VBA to Close Excel Workbook
Use VBA to Close Active Excel Workbook
- To close your current active workbook, use the following VBA code.
Sub Close_ActiveWrkbook()
ThisWorkbook.Close
End Sub
Use VBA to Close Specific Excel Workbook
- You can close a specific workbook by mentioning their file name with the file address. The file path needs to be inserted between the (“ “) in the Workbook.Close statement.
Sub Close_Wrkbook()
Dim Wrk As Workbook
Set Wrk = Workbooks.Close("C:\Folder\Sample File.xlsx")
End Sub
Use VBA to Close All the Current Excel Workbooks
- Seems you have a lot of files opened and you are tired of closing them one by one. So, you can close all the workbooks at a time simply by using the VBA codes below.
Sub Close_Wrkbook()
Dim Wrk As Workbook
Set Wrk = Workbooks.Close
End Sub
Use VBA to Close First Opened Excel Workbook
- While working with multiple files, you may need to remember what was the first one. We are here to make it happen by closing the first open workbook.
Sub Close_Wrkbook()
Dim Wrk As Workbook
Set Wrk = Workbooks(1).Close
End Sub
Use VBA to Close Excel Workbook Without Saving Changes
- To close a workbook without saving the current edits, you just need to set the SaveChanges statement as False.
Sub Close_Wrkbook()
Dim Wrk As Workbook
Set Wrk = Workbooks.Open("C:\Folder\Sample File.xlsx")
Wrk.Close SaveChanges:=False
End Sub
Use VBA to Save and Close Excel Workbook Without Prompt
- Altering the SaveChanges statement to True and closing the workbook will save your workbook without showing any prompt.
Sub Close_Wrkbook()
Dim Wrk As Workbook
Set Wrk = Workbooks.Open("C:\Folder\Sample File.xlsx")
Wrk.Close SaveChanges:=True
End Sub
Use VBA to Close Excel Workbook And Save Changes With Filename
- Interestingly, you can change the file name of a workbook before saving and closing it. To do so, insert your file name in the VBA Filename argument.
Sub Close_Wrkbook()
Dim Wrk As Workbook
Set Wrk = Workbooks.Open("C:\Folder\Sample File.xlsx")
Wrk.Close SaveChanges:=True, Filename:="New Sample File Name"
End Sub
Tags: Workbook