vba excel vba重新打开excel文件而不保存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20900495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
excel vba to reopen excel file without saving
提问by user3045580
I have the following code, it is about to reopen the current excel file.
我有以下代码,即将重新打开当前的excel文件。
Sub CloseMe()
Application.OnTime Now + TimeValue("00:00:02"), "OpenMe"
ThisWorkbook.Close False
End Sub
Sub OpenMe()
MsgBox "The file is reopened"
End Sub
I am trying to make it applicable to activeworkbook, so i change
我试图让它适用于活动工作簿,所以我改变了
ThisWorkbook.Close False
to
到
ActiveWorkbook.Close False
but it ended up close the activeworkbook but didnt reopen the file, any advise? Very sorry if this question seem silly to you.
但它最终关闭了活动工作簿但没有重新打开文件,有什么建议吗?如果这个问题对您来说很愚蠢,非常抱歉。
回答by ttaaoossuu
Try this:
尝试这个:
Sub ReOpen()
Application.DisplayAlerts = False
Workbooks.Open ActiveWorkbook.Path & "\" & ActiveWorkbook.Name
Application.DisplayAlerts = True
End Sub
回答by MikeD
I think there's a thought error ... when the workbook is closing, the contained VBA code is closing as well, so there's no code left to be executed 2 seconds later, and no object that would be the subject of any code.
我认为有一个思想错误......当工作簿关闭时,包含的 VBA 代码也在关闭,所以 2 秒后没有代码要执行,也没有任何代码的主题对象。
This will only work if your closing/reopening logic is outside the sheet you want to close/reopen, and to be more specific, residing in a workbook that remains open all the time between closing/reopening the sheet you want to reopen.
这仅在您的关闭/重新打开逻辑位于您要关闭/重新打开的工作表之外时才有效,更具体地说,驻留在工作簿中,该工作簿在关闭/重新打开要重新打开的工作表之间始终保持打开状态。
回答by tbc0
I like this, which I adapted from "How To Close And Reopen Active Workbook?"
我喜欢这个,我改编自“如何关闭和重新打开活动工作簿?”
Sub ReOpen()
ActiveWorkbook.ChangeFileAccess xlReadOnly, , False
Application.Wait Now + TimeValue("00:00:01")
ActiveWorkbook.ChangeFileAccess xlReadWrite, , True
End Sub
It seems more elegant to me and warns if there are unsaved changes. While the original uses ThisWorkbook
, I use ActiveWorkbook
like @Taosique.
它对我来说似乎更优雅,并在有未保存的更改时发出警告。虽然原始使用ThisWorkbook
,但我使用ActiveWorkbook
像@Taosique。