How to display progress bar when userform is loadi

2019-09-17 02:31发布

I have created Macro using Userform and it has many controls which are Static in nature and displays upon userform intialization. But it also has (Userform initialize code) code written add checkboxes in one of the frame dynamically using the data in the sheet1. which is taking a bit of time(say 30 sec-1 min) depending on the data present in the sheet.

during this period i want to user to be shown a progress bar of % completion.

I tried Application.Statusbar functionality but it didnt workout. So thought to go for Progressbar. Can anyone please help in this regard?

1条回答
一夜七次
2楼-- · 2019-09-17 02:53

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).

I'd follow Rorys advice though and use a listbox if you're creating potentially hundreds of controls.

Create a form called 'Progress Bar'

Give it these dimensions:
Name: ProgressBar
Height: 49.5
Width: 483.75
ShowModal: False <---- Important this bit or it won't update properly.

Add a label to the form with these dimensions:
Name: BoxProgress
Caption: BoxProgress
Height: 18
Left: 6
Top: 6
Width: 468
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.

查看更多
登录 后发表回答