绘制用户窗体后自动执行一些代码 [英] Automatically execute some code after the UserForm is drawn
问题描述
我创建了一个UserForm,要求用户填写三个字段.宏尝试在UserForm_Initialize()
事件中自动检测这些字段的值,然后在三个字段中显示找到的值,但用户可以更改它们.但是,自动检测需要花费几秒钟的时间,并且会延迟UserForm的出现.我希望UserForm在自动检测过程之前显示其字段为空白,然后让自动检测过程自动填充字段.最好的方法是什么?使UserForm为非模态将使宏在不等待用户输入的情况下运行,这是有问题的.我不想有一个自动检测"按钮:必须自动完成.
I have created a UserForm where the user is required to fill-in three fields. The macro attempts to auto-detect these fields' values in the UserForm_Initialize()
event, then displays the found values in the three fields, but the user can change them. The auto-detection takes a few seconds, though, and delays the appearance of the UserForm. I'd like the UserForm to appear with its fields blank before the auto-detection procedure, then have the auto-detection procedure fill the fields automatically. What would be the best way to do this? Making the UserForm non-modal makes the macro run without waiting for the user's input, which is problematic. I don't want to have an "auto-detect" button: this has to be done automatically.
推荐答案
使用Activate()
事件而不是Initialize()
:)
Private Sub UserForm_Activate()
End Sub
关注
谢谢!它可以工作,但是似乎有一个错误:对话框被全白绘制,直到宏完成.屏幕截图(对话框应为灰色)
Thanks! It works, but there seems to be a bug: the dialog is drawn all white until the macro completes. screenshot (the dialog should be gray)
不.这不是错误:)试试这个.如下所示添加Doevents
.
No. It is not a bug :) Try This. Add Doevents
as shown below.
Private Sub UserForm_Activate()
UserForm1.ProgressBar1.Value = 0
starttime = Timer
While Timer - starttime < 1
UserForm1.ProgressBar1.Value = (Timer - starttime) * 100
DoEvents
Wend
End Sub
HTH
Sid
这篇关于绘制用户窗体后自动执行一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!