vba 使用卸载我关闭用户表单不起作用

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

Closing a Userform with Unload Me doesn't work

excelvbaexcel-vbaexcel-2010

提问by Kian

I need to close an Excel userform using VBA when a user has clicked a submit button and operations have been carried out.

当用户单击提交按钮并执行操作时,我需要使用 VBA 关闭 Excel 用户窗体。

How can I close a Userform from itself?

如何从自身关闭用户表单?

I have tried this but it returns a 361 error.

我试过这个,但它返回一个 361 错误。

Unload Me

回答by user4059073

As specified by the top answer, I used the following in the code behind the button control.

正如最佳答案所指定的那样,我在按钮控件后面的代码中使用了以下内容。

Private Sub btnClose_Click()
    Unload Me
End Sub

In doing so, it will not attempt to unload a control, but rather will unload the user form where the button control resides. The "Me" keyword refers to the user form object even when called from a control on the user form. If you are getting errors with this technique, there are a couple of possible reasons.

这样做时,它不会尝试卸载控件,而是卸载按钮控件所在的用户窗体。即使从用户表单上的控件调用,“Me”关键字也引用用户表单对象。如果您在使用此技术时遇到错误,可能有几个原因。

  1. You could be entering the code in the wrong place (such as a separate module)

  2. You might be using an older version of Office. I'm using Office 2013. I've noticed that VBA changes over time.

  1. 您可能在错误的位置输入了代码(例如单独的模块)

  2. 您可能使用的是旧版本的 Office。我使用的是 Office 2013。我注意到 VBA 随着时间的推移而变化。

From my experience, the use of the the DoCmd.... method is more specific to the macro features in MS Access, but not commonly used in Excel VBA.

根据我的经验,DoCmd.... 方法的使用更特定于 MS Access 中的宏功能,但在 Excel VBA 中不常用。

Under normal (out of the box) conditions, the code above should work just fine.

在正常(开箱即用)条件下,上面的代码应该可以正常工作。

回答by SWa

Without seeing your full code, this is impossible to answer with any certainty. The error usually occurs when you are trying to unload a control rather than the form.

如果没有看到您的完整代码,就无法肯定地回答这个问题。当您尝试卸载控件而不是窗体时,通常会发生该错误。

Make sure that you don't have the "me" in brackets.

确保括号中没有“我”。

Also if you can post the full code for the userform it would help massively.

此外,如果您可以发布用户表单的完整代码,它将有很大帮助。

回答by Tomamais

Unload Me only works when its called from userform self. If you want to close a form from another module code (or userform), you need to use the Unload function + userformtoclose name.

Unload Me 仅在从 userform self 调用时才有效。如果要从另一个模块代码(或用户表单)关闭表单,则需要使用 Unload 函数 + userformtoclose 名称。

I hope its helps

我希望它有帮助

回答by SamG

It should also be noted that if you have buttons grouped together on your user form that it can link it to a different button in the group despite the one you intended being clicked.

还应该注意的是,如果您在用户表单上将按钮组合在一起,则它可以将其链接到组中的不同按钮,尽管您打算单击该按钮。