要是除了这些工作簿,你想要关闭并保存所有其他工作簿,则可以用这样的macro:
ub Save_and_Close_All_Files_Except_ScratchPads() 'Close all open workbooks except new unsaved files Dim 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 wb End Sub
>>>>关闭并保存所有工作簿,并给未曾保存过的工作簿自动命名
如果你想要关闭并保存所有工作簿,并且还要给那些未曾保存过的工作簿自动命名,那你可以使用以下的macro:
Sub Save_and_Close_All_Files() 'Close all open workbooks except new unsaved files Dim wb As Workbook Dim 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 wb End Sub