Excel完整诠释一键关闭所有工作簿的方法

遗忘美丽 2019-03-14 10:02:02

“如果你每天和Excel打交道,很有可能你会同时打开多个Excel文档进行跨表操作。可到了一天结束,你想要关机回家前,那么多Excel文档得一个个保存一个个关闭。有没有感到一丝丝繁琐,是的,本期火箭君来和大家介绍几种能够快速关闭并保存所有Excel文档的方法。

01

Shift + 关闭窗口按钮

想要关闭所有工作簿最快的方法就是:按住Shift键再按关闭窗口按钮

如果所有工作簿已经被保存过了,那么它们会全部被就此被关闭。

如果其中有任何工作簿没有被保存,那么Excel会提示你去保存工作簿。

其中的弹出窗口会包含“全部保存”这个选项。你可以选择点击此按钮来保存所有未被保存的工作簿。

02

添加一个“全部关闭”按钮

我们也可以在快速访问工具栏中添加“全部关闭”这个快捷按钮。它的具体作用和SHIFT+关闭窗口快捷键组合相同。

这样操作的另一个好处是,快速访问工具栏还可以使用快捷键。比如在火箭君的设置中,只要按下Alt+08就能直接实现全部关闭这个命令。

至于说,在哪里能找到“全部关闭”命令,你可以参考下图:

03

使用Macro

对于Macro的强大,这边介绍三套Macro来对应三种不同的使用需要:

>>>>

关闭所有工作簿但不保存任何工作簿

如果我们不想保存任何打开的文档,那么你可以使用这样的Macro.

Sub Close_All_Files_No_Save()'Close all open workbooks and don't saveDim wb As Workbook  'Loop through each workbook  For Each wb In Application.Workbooks        'Prevent the workbook that contains the    'code from being closed    If wb.Name <> ThisWorkbook.Name Then            'Close the workbook and don't save changes      wb.Close SaveChanges:=False        End If  Next wbEnd Sub>>>>

除了未曾保存过的工作簿,关闭并保存所有其他工作簿

如果工作簿是在本次打开中新建的,那么这样的工作簿一般被程序命名为“工作簿x”。

要是除了这些工作簿,你想要关闭并保存所有其他工作簿,则可以用这样的macro:

ub Save_and_Close_All_Files_Except_ScratchPads()'Close all open workbooks except new unsaved filesDim wb As Workbook  'Loop through each workbook  For Each wb In Application.Workbooks        'Prevent the workbook that contains the    'code from being closed    If wb.Name <> ThisWorkbook.Name Then            'Check if the file names has an extension      If InStr(Right(wb.Name, 5), ".xls") > 0 Then        wb.Close SaveChanges:=True      Else        'Do not save changes if it's a scratch pad.        wb.Close SaveChanges:=False      End If        End If  Next wbEnd Sub>>>>

关闭并保存所有工作簿,并给未曾保存过的工作簿自动命名

如果你想要关闭并保存所有工作簿,并且还要给那些未曾保存过的工作簿自动命名,那你可以使用以下的macro:

Sub Save_and_Close_All_Files()'Close all open workbooks except new unsaved filesDim wb As WorkbookDim sPath As String  'The path where the new unsaved files will be saved.  'Change this to a folder on your computer.  End with a backslash \  sPath = "C:\Users\username\Documents\Excel Campus\Scratch Pads\"  'Loop through each workbook  For Each wb In Application.Workbooks        'Prevent the workbook that contains the    'code from being closed    If wb.Name <> ThisWorkbook.Name Then            'Check if the file names has an extension      If InStr(Right(wb.Name, 5), ".xls") > 0 Then        wb.Close SaveChanges:=True      Else        'Save scratchpads in a folder        wb.Close SaveChanges:=True, _            Filename:=sPath & wb.Name & Format(Now, " yyyy-mm-dd-hhmm")      End If        End If  Next wbEnd Sub

0 阅读:2

遗忘美丽

简介:自由自在