vba 如何让 Excel 提示在关闭时保存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3311939/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:54:35  来源:igfitidea点击:

How can I make Excel prompt to save on close?

excelvba

提问by l--''''''---------''''''''''''

When you close Excel and you have edited your VBA code, it does not prompt you to Save. I just wrote 300 lines of code and it did not get saved.

当您关闭 Excel 并编辑了 VBA 代码时,它不会提示您保存。我只写了 300 行代码,但没有保存。

how do I force it to prompt when closing?

关闭时如何强制它提示?

采纳答案by Kevin Ross

It should have prompted you to save. Check you have not set SetWarnings to off somewhere in your code

它应该提示您保存。检查您没有在代码中的某处将 SetWarnings 设置为 off

回答by Diego Castro

You could Change the SetWarningssettings as sugested by Kevin.

您可以按照 Kevin的建议更改SetWarnings设置。

But what I normally do is set all my excel projects to autosaveon exit.

但我通常做的是将我所有的 excel 项目设置为在退出时自动保存

To do that, just add in the ThisWorbookmodule:

为此,只需添加ThisWorbook模块:

Code:

代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ThisWorkbook.Close SaveChanges:=True
End Sub

Of course, if a user really screws up your file, and then bails, it will save it without asking, so it could be dangerous!

当然,如果用户真的搞砸了你的文件,然后保释,它会不问而保存它,所以它可能很危险!

回答by user11593873

Add before the execution line of code:

在执行代码行前添加:

response(msgbox("Do you want to save this copy of your file?",VByesNo,"")

if response =VBYes, then

line of code to save

Else

msgbox "File was not saved",vbOK,""

Exit Sub
End if