Adding time elapsed to progress bar

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a userform called Progress which has a frame named Border, a label named Bar and another label called Text.

With this userform I would like to add time elapsed and possible time remaining?

In this thread time elapsed and time remaining is calculated, however, I can't modify it to work with my code since I don't understand the code.
https://www.mrexcel.com/forum/excel-questions/653806-progress-bar-countdown-timer-elapsed-time.html



Code:
Sub Progressbar()


Dim CurrentProgress As Double
Dim ProgressPercentage As Double
Dim BarWidth As Long


Call InitProgressBar


CurrentProgress = L / 1600
BarWidth = Progress.BorderColor.Width * CurrentProgress
ProgressPercentage = Round(CurrentProgress * 100, 0)


Progress.Bar.Width = BarWidth
Progress.Text.Caption = ProgressPercentage & "% Complete"


DoEvents


End Sub


Code:
Sub InitProgressBar()


With Progress
   .Bar.Width = 0
   .Text.Caption = "0% Complete"
   .Show vbModeless
End With


End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
Sub MyCode()
Dim L As Integer
Dim N As Integer


Dim CurrentProgress As Double
Dim ProgressPercentage As Double
Dim BarWidth As Long

Dim Time_Elapsed As Long
Dim Time_Start As Long

Time_Start = Now()


Call InitProgressBar


For L = 0 To 39
Sheets("Dash").ComboBox1.ListIndex = L


CurrentProgress = L / 1600
BarWidth = Progress.BorderColor.Width * CurrentProgress
ProgressPercentage = Round(CurrentProgress * 100, 0)

With Progress
.Bar.Width = BarWidth
.Text.Caption = ProgressPercentage & "% Complete"
.Text2.Caption = "Time elapsed: " & Time_Elapsed???
End With



For N = 0 To 39
Sheets("Dash").ComboBox2.ListIndex = N


Next N
Next L


End Sub

Thanks for your reply! I am using two FOR loops but I am only calculating the progress of the outer loop?

I have added a label called Text2 where I want to display the time elapsed.
 
Last edited:
Upvote 0
Change Time_Start to As Date and include this:
Code:
    Dim elapsedTime As Date, remainingTime As Date
    elapsedTime = Now - Time_Start
    
    If elapsedTime > 0 And ProgressPercentage > 0 Then
        remainingTime = (100 / ProgressPercentage * elapsedTime) - elapsedTime
        With Progress
            .Bar.Width = BarWidth
            .Text.Caption = ProgressPercentage & "% Complete"
            .text2.Caption = "Elapsed = " & Int(elapsedTime * 86400) & " secs, Remaining = " & Int(remainingTime * 86400) & " secs"
        End With
    End If
 
Last edited:
Upvote 0
Thank you for your reply John_w! Your code works great and I understand it.
 
Upvote 0
Glad it works! But shouldn't this line:
Code:
CurrentProgress = L / 1600
be
Code:
CurrentProgress = (L + 1) / 40
? Because your progress bar is updated inside the For L loop, which ranges from 0 to 39.

To update the progress bar inside the nested For N loop, move all the calculations and ProgressBar code inside the For N loop and change the above line to:
Code:
CurrentProgress = (L + 1) * (N + 1) / 1600
 
Upvote 0
Thank you for your reply! I'll try it tomorrow!

How would I create a function that I call with this code? That I would pass the values of N and L to?
 
Last edited:
Upvote 0
I don't understand all of the code, if you have the time could you comment your code?
 
Upvote 0
The calculations are based on the ProgressPercentage. Here is my code with comments, which I hope make sense:

Code:
    Dim elapsedTime As Date, remainingTime As Date
    
    'Elapsed time = current time - start time
    elapsedTime = Now - Time_Start
    
    'Calculate remaining time only if ProgressPercentage is greater than zero to avoid divide by zero error
    
    If elapsedTime > 0 And ProgressPercentage > 0 Then
        
        'Total estimated time is the elapsed time as a proportion of 100% of the progress so far: Total estimated time = 100% of progress / current progress% * elapsed time
        'Therefore remaining time = Total estimated time - elapsed time

        remainingTime = (100 / ProgressPercentage * elapsedTime) - elapsedTime
        
        With Progress
            .Bar.Width = BarWidth
            .Text.Caption = ProgressPercentage & "% Complete"
            
            'Convert Excel date-times to seconds by multiplying by the number of seconds in 1 day (24 * 60 * 60 = 86,400)
            'See http://www.cpearson.com/excel/datetime.htm
        
            .Text2.Caption = "Elapsed = " & Int(elapsedTime * 86400) & " secs, Remaining = " & Int(remainingTime * 86400) & " secs"
        End With
    End If
The total estimated time, and therefore the remaining time, can be simplified by factoring out the 100s:
Code:
        remainingTime = elapsedTime / CurrentProgress - elapsedTime

How would I create a function that I call with this code? That I would pass the values of N and L to?
The parameters needed to implement ProgressBar as a procedure without global variables (your L and Time_Start variables) would be the start time, the current L count and the maximum L count. Something like this, together with example calling code:

Code:
Public Sub Show_ProgressBar2()

    Dim startTime As Date, L As Long
    
    startTime = Now
    InitProgressBar
    
    For L = 1 To 1600
        ProgressBar2 startTime, L, 1600
        'Other code here to do main processing whilst progress bar updates
    Next
    
End Sub

Private Sub InitProgressBar()

    With Progress
       .Bar.Width = 0
       .Text.Caption = "0% Complete"
       .Show vbModeless
    End With

End Sub

Private Sub ProgressBar2(startTime As Date, currentCount As Long, maximumCount As Long)

    Dim CurrentProgress As Double
    Dim ProgressPercentage As Long
    Dim BarWidth As Long
    Dim elapsedTime As Date, remainingTime As Date
        
    CurrentProgress = currentCount / maximumCount
    BarWidth = Progress.Width * CurrentProgress
    ProgressPercentage = Round(CurrentProgress * 100, 0)
    elapsedTime = Now - startTime
    
    'Calculate remaining time only if ProgressPercentage is greater than zero to avoid divide by zero error
    
    If elapsedTime > 0 And ProgressPercentage > 0 Then
        
        'Total estimated time is the elapsed time as a proportion of the progress so far: Total estimated time = elapsed time / current progress
        'Therefore remaining time = Total estimated time - elapsed time
        
        remainingTime = elapsedTime / CurrentProgress - elapsedTime
        With Progress
            .Bar.Width = BarWidth
            .Text.Caption = ProgressPercentage & "% Complete"

            'Convert Excel date-times to seconds by multiplying by the number of seconds in 1 day (24 * 60 * 60 = 86,400)
            'See http://www.cpearson.com/excel/datetime.htm

            .Text2.Caption = "Elapsed = " & Int(elapsedTime * 86400) & " secs, Remaining = " & Int(remainingTime * 86400) & " secs"
        End With
    End If
    
    DoEvents
    
End Sub
 
Upvote 0
Thank you for your reply and for taking the time to comment the code! I am going it try it at once!
 
Upvote 0

Forum statistics

Threads
1,215,256
Messages
6,123,912
Members
449,132
Latest member
Rosie14

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