{"id":12057,"date":"2011-10-11T10:15:19","date_gmt":"2011-10-11T10:15:19","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=21"},"modified":"2022-08-07T20:02:37","modified_gmt":"2022-08-07T20:02:37","slug":"save-and-close-an-excel-sheet-using-macro","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2011\/10\/save-and-close-an-excel-sheet-using-macro\/","title":{"rendered":"Excel VBA Tutorial – Save and Close Excel workbook"},"content":{"rendered":"
Step by Step Guide to Save and Close Workbooks<\/h1>\n
\n
Index:<\/h2>\n\n\n
Save and close all open workbooks including current one<\/li>\n
Save and close all open workbooks without closing the current one<\/li>\n
Save and close a specific workbook<\/li>\n
Save and close a current workbook Only<\/li>\n
Close a workbook without saving it<\/li>\n<\/ol>\n<\/div>\n
While closing an Excel workbook manually, you would have noticed this confirmation popup before it closes. <\/p>\n
Closing Excel Workbook – VBA<\/p><\/div>\n
The user is asked to save the changes before closing it. When you try to close an excel workbook programmatically, then you need to provide this choice before closing it – Whether you want to save it before closing or discard the changes.<\/p>\n
This is the statement that is used to close a Workbook. ThisWorkbook.Close savechanges:=True<\/code><\/span> The parameter savechanges:=True<\/code><\/span> or savechanges:=False<\/code><\/span> is set according to your preference. \nNote:<\/strong> That ThisWorkbook<\/code> is current workbook object. That means after executing the above statement, the same workbook will be closed where the statement is executed. \nLet’s take a look at a few examples.<\/p>\n
How to save and close all open workbooks including current one<\/h1>\n
In this example, we are going to save and close all open workbooks including the current one, where this VBA code is running. Since you need to close the current workbook too, you need to make sure that the Save and Close <\/code><\/span> command is executed for the current workbook at last. You can see in the below code, that before closing any workbook from Workbooks <\/code> Object which is basically a collection of all Open Workbooks, I am checking the name of the workbook and if it is matching with the current workbook name, which is ThisWorkbook<\/code> Object. If it is not the same, then close it else move to the next Workbook. Once all the workbooks are closed except current one, you can close the current workbook.<\/p>\n
\r\nSub SaveClose()\r\nDim Wb As Workbook\r\nFor Each Wb In Workbooks\r\nIf Wb.Name <> ThisWorkbook.Name Then\r\nWb.Close savechanges:=True\r\nEnd If\r\nNext Wb\r\nThisWorkbook.Close savechanges:=True\r\nEnd Sub<\/code><\/pre>\n
How to save and close all open workbooks without closing the current one<\/h1>\n
Sometimes you need to close all the workbook before you run your macro in an excel workbook. In such case, you can use following code to close all open workbooks except the current one where the code is running.<\/p>\n
\r\nSub SaveClose()\r\nDim Wb As Workbook\r\nFor Each Wb In Workbooks\r\nIf Wb.Name <> ThisWorkbook.Name Then\r\nWb.Close savechanges:=True\r\nEnd If\r\nNext Wb\r\nEnd Sub<\/code><\/pre>\n
How to save and close a specific workbook<\/h1>\n
.close<\/code> method can be executed on any Worbook object which can close the workbook.<\/p>\n
Step by Step Guide to Save and Close Workbooks Index: Save and close all open workbooks including current one Save and close all open workbooks without closing the current one Save and close a specific workbook Save and close a current workbook Only Close a workbook without saving it While closing an Excel workbook manually, […]<\/p>\n","protected":false},"author":45,"featured_media":242685,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246],"tags":[],"class_list":["post-12057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-macro"],"yoast_head":"\n
Excel VBA Tutorial - Save and Close Excel workbook - Let's excel in Excel<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\n\n\t\n\t\n\t\n