vba 为什么将用户窗体显示为模态停止代码执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1783906/
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
Why does Showing a UserForm as Modal Stop Code Execution?
提问by Kuyenda
The following VBA code stops at Me.Show. From my tests, it seems that Me.Showstops all code execution, even if the code is inside the UserForm.
以下 VBA 代码在Me.Show. 从我的测试来看,似乎Me.Show停止所有代码执行,即使代码在用户窗体中。
This part is outside the UserForm:
这部分在 UserForm 之外:
Public Sub TestProgress()
Dim objProgress As New UserForm1
objProgress.ShowProgress
Unload objProgress
End Sub
This part is inside the UserForm:
这部分在 UserForm 里面:
Private Sub ShowProgress()
Me.Show vbModal
Dim intSecond As Integer
For intSecond = 1 To 5
Application.Wait Now + TimeValue("0:00:01")
Me.ProgressBar1.Value = intSecond / 5 * 100
Next intSecond
Me.Hide
End Sub
The code stops at Me.Show, after the UserForm is displayed. There is no error, it just discontinues executing code. It seems that the only way to execute code inside a modal UserForm in VBA is to include it in the UserForm_Activate procedure like this:
Me.Show显示用户窗体后,代码在 处停止。没有错误,它只是停止执行代码。似乎在 VBA 中的模态用户窗体中执行代码的唯一方法是将其包含在 UserForm_Activate 过程中,如下所示:
This part is outside the UserForm:
这部分在 UserForm 之外:
Public Sub TestProgress()
Dim objProgress As New UserForm1
Load objProgress
Unload objProgress
End Sub
This part is inside the UserForm:
这部分在 UserForm 里面:
Private Sub UserForm_Initialize()
Me.Show vbModal
End Sub
Private Sub UserForm_Activate()
Dim intSecond As Integer
For intSecond = 1 To 5
Application.Wait Now + TimeValue("0:00:01")
Me.ProgressBar1.Value = intSecond / 5 * 100
Next intSecond
Me.Hide
End Sub
Of course, I can't put Me.Showinside UserForm_Activate because that procedure only fires after the UserForm Show event.
当然,我不能将Me.ShowUserForm_Activate放入内部,因为该过程仅在 UserForm Show 事件之后触发。
The documentation for UserForm.ShowModalsays "When a UserForm is modal, the user must supply information or close the UserForm before using any other part of the application. No subsequent code is executed until the UserForm is hidden or unloaded."
文档UserForm.ShowModal说“当用户窗体是模态时,用户必须在使用应用程序的任何其他部分之前提供信息或关闭用户窗体。在隐藏或卸载用户窗体之前不会执行后续代码。”
I am trying to use a modal UseForm as a progress bar to prevent the user from interacting with the application while a process runs. But this will be difficult to accomplish if all my code has to be within the UserForm_Activate procedure.
我正在尝试使用模态 UseForm 作为进度条,以防止用户在进程运行时与应用程序交互。但是如果我的所有代码都必须在 UserForm_Activate 过程中,这将很难实现。
Am I missing something here? Why does all code execution stop at Me.Show?
我在这里错过了什么吗?为什么所有代码执行都停止在Me.Show?
采纳答案by Kuyenda
I think I figured this out.
我想我想通了。
After Me.Showthe UserForm_Activate event fires. If there is no code in the UserForm_Activate procedure nothing will happen because VBA is waiting for Me.Hide.
之后Me.Show的UserForm_Activate事件触发。如果 UserForm_Activate 过程中没有代码,则不会发生任何事情,因为 VBA 正在等待Me.Hide.
So the order of events is: Me.Show> UserForm_Activate> Me.Hide
所以事件的顺序是:Me.Show> UserForm_Activate>Me.Hide
Any code that I want to run must be in the UserForm_Activate procedure and must be beforeMe.Hide.
我要运行的任何代码都必须在 UserForm_Activate 过程中,并且必须在Me.Hide.
The structure is very strict, but I may be able to use that structure to my advantage.
该结构非常严格,但我也许可以利用该结构对我有利。
回答by Robert Mearns
When the form is displayed with vbModal, the code will suspend execution and wait for user interaction with the form. For example clicking a button or using a dropdown.
当表单显示为 时vbModal,代码将暂停执行并等待用户与表单交互。例如单击按钮或使用下拉菜单。
If you update the form property
如果您更新表单属性
ShowModal = False
and remove vbModalfrom your code. This will allow code execution to continue when the form is displayed.
并vbModal从您的代码中删除。这将允许在显示表单时继续执行代码。
回答by Jamie Garroch
I was searching for an answer to why I was getting the following error:
我正在寻找为什么我收到以下错误的答案:
Run time error '5': Invalid procedure call or argument
运行时错误“5”:无效的过程调用或参数
when running this line of code:
运行这行代码时:
UserForm1.Show True
even though this line works:
即使这条线有效:
UserForm1.Show False
Of course. Trueis not the same as vbModal! So the simple answer is to use the correct enumerations:
当然。True与vbModal 不同!所以简单的答案是使用正确的枚举:
UserForm1.Show vbModal
UserForm1.Show vbModeless
回答by Eduard
I think i figured it out Try doing this simple step In you form right click on the bank portion and click properties Change the "ShowModal" to False or in VBA code when you show the userform using the following code:
我想我想通了 尝试执行这个简单的步骤 在您的表单上右键单击银行部分并单击属性 当您使用以下代码显示用户表单时,将“ShowModal”更改为 False 或在 VBA 代码中:
UserForm1.Show False
UserForm1.Show False
回答by Eduard
I don't really know whats going into your mind because there are a wide variety of code for what your are asking but i hope that this can help
我真的不知道你在想什么,因为有各种各样的代码来满足你的要求,但我希望这能有所帮助
Private Sub cmdSwitch_Click() UserForm1.Hide UserForm2.Show
Private Sub cmdSwitch_Click() UserForm1.Hide UserForm2.Show
End Sub
结束子

