我在显示禁用ShowModal的Userform时遇到问题(为了能够在运行用户窗体时使用电子表格)。之前我曾使用过这种方法,但我认为它不起作用,因为userform在for循环中......
我到处寻找,我尝试过:
Userform1.Show vbModeless
Userform1.Show 0
Show Modal = False
...
事情是我的userform在for循环中所以对于不同的值,如果他们是我想要的,我打开userform。代码类似于:
for
if... then
userform.show 0
end if
next
当我这样做时,它只执行for循环中的所有操作,然后最后打开userform(在无模式模式下,但为时已晚......)。如果我在普通模式userform.show中这样做,它完美运行(我只是不能使用电子表格)
有谁知道它为什么不起作用?让我知道如果你想检查整个代码,我没有发布它,因为它有点长..
提前致谢!
答案 0 :(得分:0)
如果您想在循环(和代码)仍在运行时对您的用户表单“执行任何操作”,则需要以这种方式添加DoEvents function
:
(这是我的测试子)
Sub test_UF()
Dim i
For i = 1 To 3000
'UserForm is show when i=100
If i = 100 Then
'show it in ModeLess state to keep loop running
UserForm1.Show vbModeless
End If
'this is important!!
DoEvents
'only for test
Debug.Print i
Next
End Sub
修改根据评论,如果我现在找到你,你想要同时做到这两点:
我能想到的唯一解决方法是以下代码(请参阅内部的一些评论):
Sub qTest()
Dim i
For i = 1 To 1000
'this time we show user form twice
If i > 100 And i < 103 Then
UserForm1.Show vbModeless
End If
'to keep the loop waiting until you don't hide your UserForm
'by pressing any button on UserForm
Do While UserForm1.Visible
DoEvents
Loop
Debug.Print i
Next
End Sub