保存工作表的副本并撤销对原始的所有更改

我有一个macros修改它被执行的工作簿,我希望该macros将工作簿的副本保存到一个位置(最好由用户指定),撤销工作簿的所有更改(以便它再次处于其原始状态)并closures工作簿。

不幸的是我找不到任何关于撤销和closures部分的任何input…保存副本很容易,但如何做其余的这些事情?

我同意大部分的brettdj的回应(特别是你应该先保存文件)。 但是,BrowseForFolder函数是不必要的。 相反,您可以在2002年后的Excel版本中使用内置的Windows文件夹浏览器

Sub Example() Dim strFolder as String Application.DisplayAlerts = False Application.ScreenUpdating = False ThisWorkbook.Save With Application.FileDialog(msoFileDialogFolderPicker) .Show If .SelectedItems.Count = 1 Then strFolder = .SelectedItems(1) Else 'Quit/Show message asking to specify location End If End With 'Do everything else ThisWorkbook.SaveAs strFolder & "\" & ThisWorkbook.Name ThisWorkbook.Close (False) Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub 

closures工作簿的(False)是SaveChanges:= False的缩写,并且可能不需要closures警报

此外,如果您希望将其他工作簿的更改更改为包含代码的工作簿,则可能需要使用多个Excel文件的帮助 。 主要的教训是你可以将ActiveWorkbookreplace为ThisWorkbook,或者在打开时定义工作簿

复杂的撤销是必要的 – 为什么不直接保存工作簿在代码的开始(mods之前),进行更改,保存更改文件elswehere并closuresExcel?

更新 [代码示例,它在工作簿上的托pipe小心,它将保存该文件,并在该条件closures]

 Sub SimpleSample() Dim strFolder As String With Application .DisplayAlerts = False .ScreenUpdating = False End With 'save current file in the location it was opened from ThisWorkbook.Save 'make your mods here 'stuff 'get user directory strFolder = BrowseForFolder 'save the modified workbook to the nuser selected folder (overwrite's any early version of the same name if they exist) ThisWorkbook.SaveAs strFolder & "\" & ThisWorkbook.Name 'close the file ThisWorkbook.Close With Application .DisplayAlerts = True .ScreenUpdating = True End With End Sub Function BrowseForFolder(Optional OpenAt As Variant) As Variant ' Ken Puls, http://www.vbaexpress.com/kb/getarticle.php?kb_id=284 'Function purpose: To Browser for a user selected folder. 'If the "OpenAt" path is provided, open the browser at that directory 'NOTE: If invalid, it will open at the Desktop level Dim ShellApp As Object 'Create a file browser window at the default folder Set ShellApp = CreateObject("Shell.Application"). _ BrowseForFolder(0, "Please choose a folder", 0, OpenAt) 'Set the folder to that selected. (On error in case cancelled) On Error Resume Next BrowseForFolder = ShellApp.self.Path On Error GoTo 0 'Destroy the Shell Application Set ShellApp = Nothing 'Check for invalid or non-entries and send to the Invalid error 'handler if found 'Valid selections can begin L: (where L is a letter) or '\\ (as in \\servername\sharename. All others are invalid Select Case Mid(BrowseForFolder, 2, 1) Case Is = ":" If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid Case Is = "\" If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid Case Else GoTo Invalid End Select Exit Function Invalid: 'If it was determined that the selection was invalid, set to False BrowseForFolder = False End Function 

当您closures工作簿时,您有几个选项:

 ActiveWorkbook.Close False ' closes the active workbook without saving any changes ActiveWorkbook.Close True ' closes the active workbook and saves any changes ActiveWorkbook.Close ' closes the active workbook and lets the user decide if ' changes are to be saved or not 

我想第一个会适合你,适合你的情况。