[英]How to display Progress Bar while macro runs?
I created a ProgressBar I want to display with % while my macro runs.我创建了一个 ProgressBar,我想在宏运行时用 % 显示。
I tried the parameters UserForm1.Show vbModeless
and UserForm1.Repaint
in my main macro.我在主宏中尝试了参数
UserForm1.Show vbModeless
和UserForm1.Repaint
。
Private Sub UserForm_Activate()
Dim reminder As Long
Dim i As Long, j As Long
reminder = 0
For i = 1 To 200
UserForm1.Label2.Width = UserForm1.Label2.Width + 1
If i Mod 2 = 0 Then
reminder = reminder + 1
UserForm1.Caption1 = reminder & " % completed"
End If
For j = 1 To 150
DoEvents
Next j
Next i
Unload UserForm1
End Sub
The UserForm stops on 0% and my main macro runs to the end, then the UserForm with ProgressBar runs from 0 to 100% and closes.用户窗体在 0% 处停止,我的主宏运行到最后,然后带有 ProgressBar 的用户窗体从 0 运行到 100% 并关闭。
That can not work like this.那不能这样工作。 You need 3 functions one to show the userform progressbar 0 percent.
您需要 3 个功能之一来显示用户表单进度条 0%。 then you need a function to update your form end finaly the function to remove the form This code is NOT tested see the testing code i posted here also !
那么你需要一个函数来更新你的表单最后一个函数来删除表单这个代码没有经过测试,请参阅我在这里发布的测试代码!
Dim counter As Long
Public Sub UserForm_Activate()
counter = 0
UserForm1.Label2.Width =0
UserForm.show
End Sub
Public Sub UserForm_update()
if counter <100 then counter=counter+1
UserForm1.Label2.Width = counter
DoEvents
end sub
Public Sub UserForm_close()
counter = 0
UserForm.hide
End Sub
'somewhere in a module or class
sub calculate
call UserForm_Activate()
for i=0 to 100
call UserForm_update()
'whatever
' Sleep (200) if this very handy OS call is included somewhere
next
call UserForm_close
end sub
WORKING: I create two NON MODAL!工作:我创建了两个非模态! Forms NAMED "progressbar" ( with 1 Textbox NAMED counter) and "tester " (with 3 Buttons NAMED count hide and show)
表单 NAMED“进度条”(带有 1 个文本框 NAMED 计数器)和“测试器”(带有 3 个按钮 NAMED 计数隐藏和显示)
the code in progressbar:进度条中的代码:
Dim count As Long
Public Sub progressbar_show()
count = 0
progressbar.Show
End Sub
Public Sub progressbar_update()
If count < 100 Then count = count + 1
progressbar.counter.Text = Str(count)
DoEvents
End Sub
Public Sub progressbar_close()
counter = 0
progressbar.hide
End Sub
The code in tester: (calls routines in form progressbar)测试器中的代码:(调用表单进度条中的例程)
Private Sub count_Click()
Call progressbar.progressbar_update
End Sub
Private Sub hide_Click()
Call progressbar.progressbar_close
End Sub
Private Sub show_Click()
Call progressbar.progressbar_show
End Sub
Works as designed.按设计工作。 on the click button the textbox will be updated on each click.
在单击按钮上,文本框将在每次单击时更新。 Please note HOW i call the functions from on form to the other form.
请注意我如何从一个表单调用函数到另一个表单。 The form name has to be included.
必须包含表单名称。 The name of the controls have also to match.
控件的名称也必须匹配。 Its a good idea to rename the controls and forms.
重命名控件和表单是个好主意。 I made a autocad VBA with has 1700 controls.
我制作了一个有 1700 个控件的 autocad VBA。 Its not the fun after some weeks to imagine what commandbutton_762 actuelly may do ;)
几周后想象 commandbutton_762 实际上可能会做什么不是很有趣;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.