Monday, June 20, 2016

Excel VBA- Progress Bar

TO CREATE A PROGRESS INDICATOR BAR IN MS-EXCEL USING VBA






STEPS:
1. Create a user form resembling a progress bar
2. Double click the form and in the code window, use the following codes to enable event

Option Explicit
Private Sub UserForm_Activate()
    Dim i As Long
    Dim k As Long
    
    On Error GoTo lblErr
    Me.Caption = "Progress Bar"
    Me.Height = 150
    Me.Width = 300
    Me.lblMsgBar.Width = 0
    Me.Frame1.Height = 40
    Me.Frame1.Width = 201
    For i = 1 To 100
        For k = 1 To 10000
            Me.Frame1.Caption = i & "% Completed."
            Me.lblMsgBar.Visible = True
            Me.lblMsgBar.Height = 30
            Me.lblMsgBar.Width = 2 * i
            Me.lblMsgBar.BackColor = vbRed
        Next
        DoEvents ''Update User Form
    Next
lblErr:
    Unload Me
End Sub

The following code runs when the user tries to close the form using upper left close button.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
''    MsgBox "Sorry you must use Cancel or Exit Button"
    If Cancel = True Then
        Unload Me
    End If
End If
End Sub

The following code is used in a module to load and show form. The button named Rectangle1 exists at the worksheet which is clicked by the user:

Sub Rectangle1_Click()
    UserForm1.Show
End Sub

No comments:

Post a Comment

Hot Topics