2011-09-14 36 views
0

我有一個宏修改了它在其中執行的工作簿,我希望該宏將工作簿的副本保存到某個位置(最好由用戶指定),撤銷工作簿的所有更改(以便再次處於其原始狀態)並關閉工作簿。保存工作表的副本並撤銷對原始的所有更改

不幸的是,我無法在撤銷和關閉部分找到任何輸入...保存副本很容易,但如何做其餘的這些事情?

回答

3

我同意大部分brettdj的迴應(特別是你應該先保存文件)。但是,BrowseForFolder函數是不必要的。相反,你可以使用內置的Windows文件夾瀏覽器中的Excel 2002年後的版本

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 

的(假)在關閉工作簿是短期的調用SaveChanges:= False,並且可以刪除需要關掉警報

此外,如果您希望對包含代碼的工作簿進行更改,您可能需要help using multiple Excel files。主要的教訓是你可以用ActiveWorkbook代替ThisWorkbook,或者在打開時定義工作簿

+0

這工作非常好迄今...是否有可能更改對話框,以便用戶可以命名文件而不只是位置? –

+0

是的......你可以用msoFileDialogSaveAs替換msoFileDialogFolderPicker。那麼「strFolder」也會包含文件名,所以你可以簡單地使用ThisWorkbook.SaveAs strFolder。如果你在VBA中鍵入「Application.FileDialog(」),你會看到4個不同的選項,你可能有興趣看看它們(其他的是「FilePicker」和「Open」)。 –

1

是否需要複雜的撤銷 - 爲什麼不直接在代碼的開頭(mods之前)保存工作簿,進行更改,保存更改文件elswehere並關閉Excel?

更新 [代碼示例,它是在主持工作簿工作小心在將保存的文件,並在condtion 關閉]

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 
+0

因爲我不能回覆Ed :)所以在這裏發帖。 DialogFolderPicker上的#1公平點。 #2一旦執行此行,ThisWorkbook.SaveAs strFolder&「\」&ThisWorkbook.Name,則Workbook.Saved爲True,並且ThisWorkbook.Close上的False參數是多餘的。我使用DisplayAlerts來抑制任何潛在的文件覆蓋消息,它當然也會抑制修改的代碼進程中的任何表單刪除 – brettdj

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 

我想第一個會適合你,適合你的情況。

相關問題