如何让VBA代码等到关闭vbModeless用户窗体 [英] How to have VBA code wait until vbModeless userform is closed
问题描述
我想使用无格式的用户窗体,以便用户可以在回答用户窗体上的问题之前浏览excel工作表.我需要暂停或循环执行代码,直到关闭用户窗体(隐藏或卸载).
I'd like to use a formless userform so the user can navigate the excel sheet before answering the question on the userform. I need to pause or loop the code until the userform is closed (hidden or unloaded).
Similar issue to this: How can I wait for a specific code to run while when form is closed and is set to vbModeless? but the solution here does not work for my application; My userform is opened in the middle of a long subroutine which needs to finish executing after the userform is closed.
Dim popupActive as Boolean
popupActive = True
StartingSINT_Popup.Show vbModeless 'Open userform
'have VBA code wait until userform is closed
wait until popupActive = False 'set to false with OK button on userform
'continue code with info input inside StartingSINT_Popup userform
推荐答案
我没有编写以下函数,但已经使用了很长时间,并且可以正常工作.
I didn't author the following function, but I have used it for a long time and it works.
Private Function IsLoaded(ByVal formName As String) As Boolean
Dim frm As Object
For Each frm In VBA.UserForms
If frm.Name = formName Then
IsLoaded = True
Exit Function
End If
Next frm
IsLoaded = False
End Function
您将需要对字符串名称进行硬编码,而不要使用表单的.Name
属性,因为该表单可能尚未加载且不包含此属性.
You will need to hardcode the string name, and not use the .Name
property of the form because the form may not be loaded yet and not contain this property.
以下是如何使用此功能的小片段:
Here is a small snippet of how you can use this function:
Do While IsLoaded("StartingSINT_Popup")
Debug.Print Time; " StartingSINT_Popup Is Loaded!"
Loop
这篇关于如何让VBA代码等到关闭vbModeless用户窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!