VBA display progress (e.g. in status bar) for huge iteration

gifariz

Board Regular
Joined
May 2, 2021
Messages
128
Office Version
  1. 365
Platform
  1. Windows
I have large data up to 1 million rows. My purpose is to show the iteration progress of VBA calculation, e.g. in Application.StatusBar.
I tried like this below, to update status bar only when progress is increased 1%. But the status bar is still updated in every single iteration, that is 1/1000000 or 0.0001% progress increment (heavily slowing the macro), I don't know why.
VBA Code:
Private Sub Show_Progress()

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.EnableEvents = False
   
    Dim i as long, Ln As Long
    Dim prog As Long, prog2 As Long

    Ln = 100000

    For i = 0 To Ln - 1
        prog = Int((i + 1) / Ln * 100)
        If prog > prog2 Then
            Application.StatusBar = "Calculating data (" & i + 1 & "/" & Ln & ")"
        Else
            prog2 = prog
        End If
        'Actually do something, but do nothing for example
    Next i
   
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub

The turning off calculation, screen updating, and disabling events not really helpful.

Any solution please? Thank you
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This will update the status bar in every 10K iteration:
VBA Code:
Sub gifariz()

n = 1000000
m = 10000
For i = 1 To n
    
    If i Mod m = 0 Then
        Application.StatusBar = "Progress: " & 100 * i / n & "%"
    End If

Next

End Sub
 
Upvote 0
Solution
This will update the status bar in every 10K iteration:
VBA Code:
Sub gifariz()

n = 1000000
m = 10000
For i = 1 To n
   
    If i Mod m = 0 Then
        Application.StatusBar = "Progress: " & 100 * i / n & "%"
    End If

Next

End Sub
Works like a charm, thank you!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top