[VBA] 如何 使用 ProgressBar 進度條

[VBA] 如何 使用 ProgressBar 進度條

1.在VBE介面插入一個自訂表單

快照-200962918641

 

2.在工具箱中新增 ProgressBar 控制項元件(mscomctl.ocx),並調整控制項大小

快照-2009629173246

3.加入以下程式碼,請依個人需求更改

Const ColValue As Integer = 100
Const RowValue As Integer = 100
Private Sub CommandButton1_Click()
    Dim i As Integer, j As Integer, k As Long
    Dim MinValue As Integer, MaxValue As Integer
    UserForm1.Show 0
    Cells.Clear
    On Error Resume Next
    'Application.ScreenUpdating = False
    With UserForm1.ProgressBar1
        '設定最小
        .Min = 1
        '設定最大
        .Max = RowValue * ColValue
        '進度條樣式
        .Scrolling = 1
        For i = 1 To RowValue
            For j = 1 To ColValue
                Cells(i, j).Value = k
                Cells(i, j).Select
                .Value = k
                UserForm1.Caption = "已完成" & k / 100 & "%, 請稍候!"
                k = k + 1
                'DoEvents
            Next j
        Next i
    End With
    Cells(1, 1).Select
    Application.ScreenUpdating = True
    Unload UserForm1
End Sub

快照-200962918358

 

 

範例下載:ProgressBar .rar

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo