在卸載Me時關閉Userform是無效的

[英]Closing a Userform with Unload Me doesn't work


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 userform。

How can I close a Userform from itself?

如何關閉Userform ?

I have tried this but it returns a 361 error.

我已經嘗試過了,但是它返回一個361錯誤。

Unload Me

3 个解决方案

#1


20  

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.

    您可能正在使用舊版本的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.

在正常情況下(開箱即用),上面的代碼應該可以正常工作。

#2


9  

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.

另外,如果您可以發布userform的完整代碼,這將會非常有幫助。

#3


4  

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.

卸載Me只在從userform self調用時有效。如果要從另一個模塊代碼(或userform)中關閉表單,需要使用卸載函數+ userformtoclose名稱。

I hope its helps

我希望它能幫助


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/02/28/72fc5bf83c0eed0720de0c2f638b7df1.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com