简体   繁体   English

在VBA Excel中加载用户窗体时如何显示进度栏

[英]How to display progress bar when userform is loading in VBA excel

I have created Macro using Userform and it has many controls which are Static in nature and displays upon userform intialization. 我使用Userform创建了Macro,它具有许多本质上是静态的控件,并在用户窗体初始化时显示。 But it also has (Userform initialize code) code written add checkboxes in one of the frame dynamically using the data in the sheet1. 但是它也具有(用户窗体初始化代码)代码,该代码使用sheet1中的数据动态地在其中一个框架中添加了复选框。 which is taking a bit of time(say 30 sec-1 min) depending on the data present in the sheet. 这需要一些时间(例如30秒至1分钟),具体取决于工作表中的数据。

during this period i want to user to be shown a progress bar of % completion. 在此期间,我希望向用户显示进度完成百分比。

I tried Application.Statusbar functionality but it didnt workout. 我尝试了Application.Statusbar功能,但没有锻炼。 So thought to go for Progressbar. 所以想去Progressbar。 Can anyone please help in this regard? 任何人都可以在这方面提供帮助吗?

This is the progress bar I've used for the last five or six years (originally posted in http://www.mrexcel.com/forum/excel-questions/527468-progress-bar.html ). 这是我过去五年或六年使用的进度条(最初发布在http://www.mrexcel.com/forum/excel-questions/527468-progress-bar.html )。

I'd follow Rorys advice though and use a listbox if you're creating potentially hundreds of controls. 不过,如果您要创建数百个控件,我会遵循Rorys的建议并使用一个列表框。

Create a form called ' Progress Bar ' 创建一个名为“ 进度栏 ”的表单

Give it these dimensions: 给这些尺寸:
Name: ProgressBar 名称:ProgressBar
Height: 49.5 高度:49.5
Width: 483.75 宽度:483.75
ShowModal: False <---- Important this bit or it won't update properly. ShowModal:错误<----重要的一点,否则它将无法正确更新。

Add a label to the form with these dimensions: 使用以下尺寸向表单添加标签:
Name: BoxProgress 名称:BoxProgress
Caption: BoxProgress 标题:BoxProgress
Height: 18 身高:18
Left: 6 左:6
Top: 6 最高:6
Width: 468 宽:468
BackColour: &H008080FF& BackColour:&H008080FF&

In a normal module add this procedure: 在普通模块中,添加以下过程:

Sub UpdateProgressBar(n As Long, m As Long, Optional DisplayText As String)
'// DarkSprout April08
'// Omit DisplayText to display progress percentage
On Error GoTo ERR_HANDLE

If n >= m Then
    ProgressBar.Hide
Else
    If ProgressBar.Visible = False Then ProgressBar.Show
    ProgressBar![BoxProgress].Caption = IIf(DisplayText = "", Round(((n / m) * 10000) / 100) & "%", DisplayText)
    ProgressBar![BoxProgress].Width = (n / m) * 468
    DoEvents
End If
Exit Sub

ERR_HANDLE:
    Err.Clear
    ProgressBar.Hide
End Sub

Use it in your code like this: 像这样在您的代码中使用它:

Sub test()

    Dim x As Long

    For x = 1 To 100
        UpdateProgressBar x, 100
    Next x

End Sub

You'll need to call the procedure every time you want the progress bar to update. 每次要更新进度条时,都需要调用该过程。

The variables: 变量:
m represents the maximum number the bar will reach and n represents the current value to display. m表示条形将达到的最大数量, n表示要显示的当前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM