保存工作表的副本并撤销对原始文件的所有更改
Posted
技术标签:
【中文标题】保存工作表的副本并撤销对原始文件的所有更改【英文标题】:Save copy of worksheet and revoke all changes to the original 【发布时间】:2011-09-14 10:20:07 【问题描述】:我有一个宏可以修改执行它的工作簿,我希望该宏将工作簿的副本保存到某个位置(最好由用户指定),撤销对工作簿的所有更改(所以它在其原始位置再次声明)并关闭工作簿。
不幸的是,我在撤销和关闭部分找不到任何输入...保存副本很容易,但是如何做其余的事情呢?
【问题讨论】:
【参考方案1】:我同意 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 的缩写,可能不需要关闭警报
此外,如果您希望对包含代码的工作簿进行不同的更改,您可能需要help using multiple Excel files。主要教训是您可以用 ActiveWorkbook 替换 ThisWorkbook,或在打开时定义工作簿
【讨论】:
到目前为止效果很好...是否可以更改对话框以便用户可以命名文件而不仅仅是位置? 是的...您可以将 msoFileDialogFolderPicker 替换为 msoFileDialogSaveAs。然后“strFolder”也将包含文件名,因此您可以简单地使用 ThisWorkbook.SaveAs strFolder。如果您在 VBA 中键入“Application.FileDialog(”。您将看到 4 个不同的选项。您可能有兴趣查看它们(其他是“FilePicker”和“Open”)【参考方案2】:是否有必要进行复杂的撤销 - 为什么不将工作簿原样保存在代码开头(在 mod 之前),进行更改,将更改文件保存在其他地方并关闭 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
【讨论】:
在这里发帖,因为我还不能回复 Ed :)。 #1 DialogFolderPicker 上的公平点。 #2 一旦执行了这一行,ThisWorkbook.SaveAs strFolder & "\" & ThisWorkbook.Name,那么 Workbook.Saved 为 True,ThisWorkbook.Close 上的 False 参数是多余的。我使用 DisplayAlerts 来抑制任何潜在的文件覆盖消息,它当然也会抑制修改后的代码过程中的任何工作表删除等【参考方案3】:关闭工作簿时,您有多种选择:
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
我想第一个会适合你并且适合你的情况。
【讨论】:
以上是关于保存工作表的副本并撤销对原始文件的所有更改的主要内容,如果未能解决你的问题,请参考以下文章