vba 用“X”关闭用户窗体并退出 Excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26381382/
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
Close Userform with "X" and exit Excel
提问by MVAmorim
I'm creating a Userform and I want the users to closes the userform through the "X" in the corner and by doing that, I want the Excel to close/exit. I tryied the UserForm_Deactivate(), but it's not working or calling the UserForm_Deactivate() when "X" is pressed. Heres the code.
我正在创建一个用户表单,我希望用户通过角落的“X”关闭用户表单,通过这样做,我希望 Excel 关闭/退出。我尝试了 UserForm_Deactivate(),但是当按下“X”时它不起作用或调用 UserForm_Deactivate()。这是代码。
Private Sub UserForm_Deactivate()
Application.Quit
End Sub
I also tryied Workbooks(1).Close and ActiveWorkbook.Close, none worked.
我还尝试了 Workbooks(1).Close 和 ActiveWorkbook.Close,都没有工作。
My userform is named by "Organizador", I don't know if it makes any diference since I tryied Organizador_Deactivate() and nothing happened.
我的用户表单以“Organizador”命名,我不知道它是否有任何区别,因为我尝试了 Organizador_Deactivate() 并且什么也没发生。
Thanks in advance
提前致谢
回答by Dave
You need to use UserForm_Terminate()
您需要使用 UserForm_Terminate()
回答by Buks
Also if you put "End" in the place of "Application.Quit" it will stop the program and not continue running extra commands in the background that was not completed fully especially if you use "If" functions across "Private Subs" so it will then look like this
此外,如果您将“End”放在“Application.Quit”的位置,它将停止程序并且不会继续在未完全完成的后台运行额外的命令,特别是如果您在“Private Subs”中使用“If”函数,那么它然后看起来像这样
Private Sub UserForm_Terminate()
End
End Sub