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

Excel 保存工作表副本并撤消对原始工作表的所有更改,excel,vba,Excel,Vba,我有一个宏,用于修改在其中执行的工作簿,我希望该宏将工作簿的副本保存到某个位置(最好由用户指定),撤消对工作簿的所有更改(使其再次处于原始状态)并关闭工作簿 不幸的是,我找不到任何关于撤销和关闭部分的输入。。。保存一份副本非常简单,但是如何完成其余的工作呢?是否需要复杂的撤销操作?为什么不将工作簿保存为代码开头的状态(在mods之前),进行更改,将更改文件保存在此处并关闭Excel 更新 [在托管在中的工作簿上工作的代码示例注意:它将保存文件并在该条件下关闭文件] Sub SimpleSampl

我有一个宏,用于修改在其中执行的工作簿,我希望该宏将工作簿的副本保存到某个位置(最好由用户指定),撤消对工作簿的所有更改(使其再次处于原始状态)并关闭工作簿


不幸的是,我找不到任何关于撤销和关闭部分的输入。。。保存一份副本非常简单,但是如何完成其余的工作呢?

是否需要复杂的撤销操作?为什么不将工作簿保存为代码开头的状态(在mods之前),进行更改,将更改文件保存在此处并关闭Excel

更新 [在托管在中的工作簿上工作的代码示例注意:它将保存文件并在该条件下关闭文件]

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

复杂的撤销是必要的吗?为什么不按代码开头的方式保存工作簿(在mods之前),进行更改,将更改文件保存在此处,然后关闭Excel

更新 [在托管在中的工作簿上工作的代码示例注意:它将保存文件并在该条件下关闭文件]

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

关闭工作簿时,有几个选项:

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

我想第一个会适合您并适合您的情况。

关闭工作簿时,您有几个选项:

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

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

我同意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
关闭工作簿时的(False)是SaveChanges的缩写:=False,可能不需要关闭警报


此外,如果您希望将包含代码的工作簿更改为其他工作簿,则可能需要。主要的教训是,您可以用ActiveWorkbook替换此工作簿,或者在打开时定义工作簿。我同意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
关闭工作簿时的(False)是SaveChanges的缩写:=False,可能不需要关闭警报


此外,如果您希望将包含代码的工作簿更改为其他工作簿,则可能需要。主要的教训是,您可以用ActiveWorkbook替换此工作簿,或者在打开时定义工作簿

在此处发布,因为我还无法回复Ed:)#DialogFolderPicker上有1个公平点#2执行此行后,ThisWorkbook.SaveAs strFolder&“\”&ThisWorkbook.Name,然后是Workbook.Saved,ThisWorkbook.Close上的False参数是多余的。我使用DisplayAlerts来抑制任何潜在的文件覆盖消息,它当然也会抑制修改后的代码过程中的任何工作表删除etcPosting,因为我还不能响应Ed:)#DialogFolderPicker上有1个公平点#2执行此行后,ThisWorkbook.SaveAs strFolder&“\”&ThisWorkbook.Name,然后是Workbook.Saved,ThisWorkbook.Close上的False参数是多余的。我使用DisplayAlerts来抑制任何潜在的文件覆盖消息,它当然也会抑制修改代码过程中的任何工作表删除等。到目前为止,它的效果非常好。。。是否可以更改对话框,以便用户可以命名文件而不仅仅是位置?是的…您可以将msoFileDialogFolderPicker替换为msoFileDialogSaveAs。然后,“strFolder”还将包括文件名,因此您可以简单地使用ThisWorkbook.SaveAs strFolder。如果您键入“Application.FileDialog”(“在VBA中,您将看到4个不同的选项。您可能有兴趣查看它们(其他选项为“FilePicker”和“Open”)到目前为止,这非常好…是否可以更改对话框,以便用户可以命名文件而不仅仅是位置?是的…您可以将msoFileDialogFolderPicker替换为msoFileDialogSaveAs。然后“strFolder”还将包括文件名,因此您可以简单地使用ThisWorkbook.SaveAs strFolder。如果键入“Application.FileDialog(”在VBA中,您将看到4个不同的选项。您可能有兴趣查看它们(其他选项为“文件选择器”和“打开”)