Assigning Clients a random number I have a program that creates 100,000 objects of class Client, puts them into array and then goes through that array 100 times, each time assigning each Client a different random number through Rnd() function:

Main sub:  

Sub start()
    Dim i As Long
    Dim j As Long

    Dim clientsColl() As Client
    ReDim clientsColl(1 To 100000) As Client

    For j = 1 To 100000
        Set clientsColl(j) = New Client
        clientsColl(j).setClientName = "Client_" &amp; j

        Application.StatusBar = "Getting client " &amp; j

        DoEvents
    Next


    Dim tempCount As Long
    Dim clientCopy As Variant
    For i = 1 To 100
        tempCount = 0
        For Each clientCopy In clientsColl
            tempCount = tempCount + 1

            clientCopy.generateRandom

            'Application.StatusBar = "Calculating " &amp; i &amp; ": " &amp; tempCount &amp; "/" &amp; 100000  '(1)

            'DoEvents
        Next

        Application.StatusBar = "Calculating " &amp; i

        DoEvents
    Next

    MsgBox ("done")
End Sub


Client class:

Option Explicit

Dim clientName As String
Dim randomNumber As Double

Public Sub generateRandom()
    randomNumber = Rnd()
End Sub

Public Property Get getClientName()
    getClientName = clientName
End Property

Public Property Let setClientName(value As String)
    clientName = value
End Property


The problem is, the execution time depends on whether or not line (1) is commented out. If it's executed, the status bar gets renewed, but the execution time is very slow. If it's not executed, the program gets done really fast. Why does this happen?
Your question is why does it take a long time to update the Application.StatusBar 10,000,000 times?  The answer is that you are updating the Application.StatusBar 10,000,000 times.

Using the Timer from TheSpreadSheetGuru, I calculated that it takes roughly 1 sec to do 10,000 updates.  So it will take roughly 10,000,000/10,000/60 minutes just to do the updates.  That is roughly 16.667 minutes.



Sub CalculateRunTime_Seconds()
'PURPOSE: Determine how many seconds it took for code to completely run
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
    Dim x As Long
    Dim StartTime As Double
    Dim SecondsElapsed As Double

    'Remember time when macro starts
    StartTime = Timer

    '*****************************
    'Insert Your Code Here...
    For x = 1 To 1000
        Application.StatusBar = x
    Next
    '*****************************

    'Determine how many seconds code took to run
    SecondsElapsed = Round(Timer - StartTime, 2)

    'Notify user in seconds
    MsgBox "This code ran successfully in " &amp; SecondsElapsed &amp; " seconds", vbInformation

End Sub