我希望宏撤消自上次打开/保存工作簿以来的所有更改。
我认为这就像重新打开工作簿一样容易,类似于以下答案: https://stackoverflow.com/a/44244262/1473412
但是,当我使用时:
Workbooks.Open(ThisWorkbook.FullName)
从字面上看,什么都没有发生。没有警报,没有错误,没有变化。好像该命令只是被忽略。在最新版本的Excel中,此行为是否已更改?
在最新版本的excel(2016)中将工作簿还原到以前保存状态的最简单方法是什么?
谢谢
答案 0 :(得分:1)
尝试以下解决方法:
Public Sub ReOpenWithoutSave()
Application.EnableEvents = False
ThisWorkbook.Saved = True
ThisWorkbook.ChangeFileAccess xlReadOnly, , False
Application.Wait Now + TimeValue("00:00:01")
ThisWorkbook.ChangeFileAccess xlReadWrite, , True
Application.EnableEvents = True
End Sub
ThisWorkbook
标记为已保存(但不保存)。ThisWorkbook
为只读,并等待1秒钟使其继续进行ThisWorkbook
再次可写(这会自动重新打开)答案 1 :(得分:1)
逻辑:
不保存就关闭Excel文件,只需重新打开即可。但是,如何从同一文件重新打开它?
这是您要尝试的吗?
Sub Sample()
Dim file As String
file = ThisWorkbook.Path & "\DeletemeLater.vbs"
Sheet1.Range("A2").Value = "Blah Blah" '<~~ This change will not be saved
'~~> Create a vbscript file
Open file For Output As #1
Print #1, "WScript.Sleep 300"
Print #1, "Set oExcelApp = GetObject(,""Excel.Application"")"
Print #1, "oExcelApp.Visible=True"
Print #1, "Set oWB = oExcelApp.Workbooks.Open(" & Chr(34) & ThisWorkbook.FullName & Chr(34) & ")"
Close #1
Shell "wscript " & Chr(34) & file & Chr(34), vbNormalFocus
ThisWorkbook.Close (False)
End Sub
还有一件事。将此添加到工作簿代码区域
Private Sub Workbook_Open()
On Error Resume Next
Kill ThisWorkbook.Path & "\DeletemeLater.vbs"
On Error GoTo 0
End Sub