为什么将用户窗体显示为模式会停止代码执行?
以下 VBA 代码在 Me.Show
处停止。从我的测试来看,Me.Show
似乎停止了所有代码执行,即使代码位于用户窗体内。
这部分位于 UserForm 外部:
Public Sub TestProgress()
Dim objProgress As New UserForm1
objProgress.ShowProgress
Unload objProgress
End Sub
这部分位于 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
显示 UserForm 后,代码在 Me.Show
处停止。没有错误,它只是停止执行代码。似乎在 VBA 中执行模态 UserForm 内部代码的唯一方法是将其包含在 UserForm_Activate 过程中,如下所示:
这部分位于 UserForm 之外:
Public Sub TestProgress()
Dim objProgress As New UserForm1
Load objProgress
Unload objProgress
End Sub
这部分位于 UserForm 内部< /em>:
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
当然,我不能将 Me.Show
放入 UserForm_Activate 中,因为该过程仅在 UserForm Show 事件之后触发。
UserForm.ShowModal 的文档说“当 UserForm 是模态的时,用户必须在使用应用程序的任何其他部分之前提供信息或关闭 UserForm。在 UserForm 之前不会执行后续代码被隐藏或卸载。”
我尝试使用模态 UseForm 作为进度条,以防止用户在进程运行时与应用程序交互。但如果我的所有代码都必须位于 UserForm_Activate 过程中,这将很难完成。
我在这里错过了什么吗?为什么所有代码执行都停止在 Me.Show
处?
The following VBA code stops at Me.Show
. From my tests, it seems that Me.Show
stops all code execution, even if the code is inside the UserForm.
This part is outside the UserForm:
Public Sub TestProgress()
Dim objProgress As New UserForm1
objProgress.ShowProgress
Unload objProgress
End Sub
This part is inside the 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:
This part is outside the UserForm:
Public Sub TestProgress()
Dim objProgress As New UserForm1
Load objProgress
Unload objProgress
End Sub
This part is inside the 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.Show
inside UserForm_Activate because that procedure only fires after the UserForm Show event.
The documentation for UserForm.ShowModal
says "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."
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.
Am I missing something here? Why does all code execution stop at Me.Show
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当使用
vbModal
显示表单时,代码将暂停执行并等待用户与表单交互。例如,单击按钮或使用下拉菜单。如果您更新表单属性
并从代码中删除
vbModal
。这将允许在显示表单时继续执行代码。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.If you update the form property
and remove
vbModal
from your code. This will allow code execution to continue when the form is displayed.我正在寻找为什么出现以下错误的答案:
运行时错误“5”:运行此行代码时
:即使此行有效:
当然。 True 与 vbModal 不同!所以简单的答案是使用正确的枚举:
I was searching for an answer to why I was getting the following error:
when running this line of code:
even though this line works:
Of course. True is not the same as vbModal! So the simple answer is to use the correct enumerations:
我想我已经明白了这一点。
在
Me.Show
之后,将触发 UserForm_Activate 事件。如果 UserForm_Activate 过程中没有代码,则不会发生任何事情,因为 VBA 正在等待Me.Hide
。所以事件的顺序是:
Me.Show
> >UserForm_Activate
> >Me.Hide
我想要运行的任何代码都必须位于 UserForm_Activate 过程中,并且必须位于
Me.Hide
之前。该结构非常严格,但我也许可以利用该结构来发挥我的优势。
I think I figured this out.
After
Me.Show
the UserForm_Activate event fires. If there is no code in the UserForm_Activate procedure nothing will happen because VBA is waiting forMe.Hide
.So the order of events is:
Me.Show
>UserForm_Activate
>Me.Hide
Any code that I want to run must be in the UserForm_Activate procedure and must be before
Me.Hide
.The structure is very strict, but I may be able to use that structure to my advantage.