我有一个宏来修改它在其中执行的工作簿,我希望该宏将工作簿的副本保存到某个位置(最好由用户指定),撤消对工作簿的所有更改(因此它是原始的再次声明)并关闭工作簿。
不幸的是我找不到关于撤销和关闭部分的任何输入...保存副本很简单,但如何做其余的事情呢?
答案 0 :(得分:3)
我同意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,或者在打开时定义工作簿
答案 1 :(得分:1)
复杂的撤销是否必要 - 为什么不像在代码开头那样保存工作簿(在mod之前),进行更改,保存更改文件并关闭Excel?
<强>更新强> [在 托管的工作簿上运行的代码示例,请注意保存文件并在该条件中关闭 < / em>]
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
答案 2 :(得分:0)
关闭工作簿时,您有以下几种选择:
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
我想第一个适合你并适合你的情况。