VBA macros · Utilities

VBA macro to show a progress bar in the Excel status bar

Show a text progress bar with percentage in Excel's status bar while a long macro runs, and restore the status bar safely afterwards, even after errors.

Windows + Mac

A macro that runs for a minute with screen updating off looks exactly like a frozen Excel. People press Esc, click around or kill Excel in Task Manager. A progress message in the status bar at the bottom of the window fixes that with almost no extra work. This page gives you a reusable ShowProgress procedure and a demo loop that shows how to call it.

VBA
Option Explicit

Sub ProgressDemo()
    Dim i As Long, total As Long
    Dim x As Double
    Dim oldStatusBar As Boolean

    total = 200000
    oldStatusBar = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    Application.ScreenUpdating = False
    On Error GoTo CleanUp

    For i = 1 To total
        x = x + Sqr(i)                       ' your real work goes here
        If i Mod 1000 = 0 Or i = total Then
            ShowProgress i, total, "Processing"
        End If
    Next i

CleanUp:
    Application.StatusBar = False             ' give the status bar back to Excel
    Application.DisplayStatusBar = oldStatusBar
    Application.ScreenUpdating = True
    If Err.Number <> 0 Then MsgBox "Stopped: " & Err.Description, vbExclamation
End Sub

Public Sub ShowProgress(ByVal done As Long, ByVal total As Long, _
                        Optional ByVal label As String = "Working")
    Const BAR_LEN As Long = 20
    Dim filled As Long

    If total <= 0 Then Exit Sub
    filled = Int(done / total * BAR_LEN)
    Application.StatusBar = label & "  " & _
        String(filled, ChrW(9608)) & String(BAR_LEN - filled, ChrW(9617)) & _
        "  " & Format(done / total, "0%") & "  (" & done & " of " & total & ")"
    DoEvents                                  ' let Excel repaint the status bar
End Sub
Show a progress bar in the status bar

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick ProgressDemo and click Run.
  5. Watch the bottom-left corner of the Excel window: the bar fills up and disappears when the loop ends.
  6. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

How it works

Application.StatusBar accepts any text, which Excel shows in the status bar instead of its own messages. ShowProgress builds that text: a label, a bar made of full blocks (ChrW(9608)) and light shaded blocks (ChrW(9617)), the percentage and the count. String(n, character) repeats a character n times, so with 20 blocks each block stands for 5%.

The demo updates the bar only every 1,000 steps (i Mod 1000 = 0) and on the last step. Updating the status bar is not free; doing it 200,000 times would make the macro noticeably slower. Aim for roughly one update per percent. DoEvents gives Excel a moment to repaint the bar and respond to Esc; the status bar keeps updating even while screen updating is off.

Setting Application.StatusBar = False hands control back to Excel, which then shows Ready again. It sits after the CleanUp label, which both the normal end of the loop and the error handler reach, so the status bar is never left showing an old message. The original DisplayStatusBar setting is restored as well. This clean-up pattern is explained in Error handling and debugging in VBA.

Change it

To use it in your own macro, copy ShowProgress into the same module, then call it from inside your loop, for example once per file in a folder loop or once per sheet:

VBA
    Dim k As Long
    For Each ws In ActiveWorkbook.Worksheets
        k = k + 1
        ShowProgress k, ActiveWorkbook.Worksheets.Count, "Sheet " & ws.Name
        ' ... work on ws ...
    Next ws
    Application.StatusBar = False
Call ShowProgress from your own loop

If you do not know the total in advance, show a running count only: Application.StatusBar = "Rows done: " & i. For a more visible indicator, a small UserForm with a coloured label that grows works too; see UserForms: build a data-entry form in VBA. It is more work and needs DoEvents in the same way. To make the macro faster in the first place, read Make your macros fast: speed optimization in VBA.

What can go wrong

Watch out. If your own macro stops with an unhandled error before it reaches Application.StatusBar = False, the last progress message stays in the status bar until Excel is restarted or another macro resets it. Type Application.StatusBar = False in the Immediate window (Ctrl+G) to clear it.

DoEvents lets the user click and type while the macro runs. If they edit cells or close the workbook in the middle, your loop may fail. For critical macros, call DoEvents only every few hundred steps, or skip it and accept that the bar may update less smoothly.

Tip. Works on Windows and Mac. If the block characters show as boxes on an older system, replace them with | and . for a plain-text bar.

Related: VBA macro to refresh all pivot tables in Excel, Make your macros fast: speed optimization in VBA, Error handling and debugging in VBA.

More vba macros