Home » excel » excel vba – Progress bar while running macro

excel vba – Progress bar while running macro

Posted by: admin May 14, 2020 Leave a comment


This is a macro that works on all the files of my workbook and executes the macro wrap
on all the sheets if the sheet is visible. I wanted to show a progress bar to show the progress while macro is running..

Sub execute()
Application.ScreenUpdating = False
Application.Cursor = xlWait
' makes sure that the statusbar is visible
Application.DisplayStatusBar = True
'add your message to status bar
Application.StatusBar = "Formatting Report..."

    Call Delete_EmptySheets
    Dim WS_Count As Integer
    Dim i As Worksheet

 ' Set WS_Count equal to the number of worksheets in the active
 ' workbook.

 WS_Count = ActiveWorkbook.Worksheets.Count

' Begin the loop.

For Each i In Worksheets
If Not i.Visible = xlSheetVeryHidden Then
  Call wrap
End If
Next i

Application.Cursor = xlDefault
' gives control of the statusbar back to the programme
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub

For the same I have used a userform with a label but it executes only before or after the macro execute

Private Sub UserForm_Activate()
 Call ShowProgressBarWithoutPercentage
End Sub

Sub ShowProgressBarWithoutPercentage()
Dim Percent As Integer
Dim PercentComplete As Single
Dim MaxRow, MaxCol As Integer
Dim iRow, iCol As Integer
MaxRow = 500
MaxCol = 500
Percent = 0
'Initially Set the width of the Label as Zero
UserForm1.Label1.Width = 0
For iRow = 1 To MaxRow
    For iCol = 1 To MaxCol
        Worksheets("Sheet1").Cells(iRow, iCol).Value = iRow * iCol

    PercentComplete = iRow / MaxRow
    UserForm1.Label1.Width = PercentComplete * UserForm1.Width

Unload UserForm1
End Sub

Can someone show a method to show the progressbar when macro is running in background?

How to&Answers:

The problem could be your Application.ScreenUpdating = False. You could update the screen periodically, but that might negate the benefits of having it set to False in the first place. The status bar still updates though, so you could write something like the following to the status bar.

0%  |
10% ||||||

And update that as the macro runs.

25%  ||||||||||||||
50%  ||||||||||||||||||||||||||||
100% ||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Here’s an example:

Sub StatusBarPercent(Percent As Double)
    Dim i As Long
    Dim Status As String
    Percent = Percent * 100
    Status = "Formatting Report...  " & Percent & "% "
    For i = 0 To Percent
        Status = Status & "|"
    Application.StatusBar = Status
End Sub