Option Explicit
Sub Estimated_time()
Dim StartValue 'Your startvalue in the loop, change it to your variable
Dim StopValue 'Your stopvalue in the loop, change it to your variable
Dim percent5, percent10, percent20, percent50, percent80, percent95
Dim tStart, t1, t2, t3, timeleft
Dim mins, secs
Dim MyTimer 'Dummy variable - remove it when implemented in your code
StopValue = 600
StartValue = 1
'This is only to make the code easier to read in the loop
'If you need more or less calculationpoints you can just add or delete them
percent5 = (StopValue - StartValue) / 20
percent10 = (StopValue - StartValue) / 10
percent20 = (StopValue - StartValue) / 5
percent50 = (StopValue - StartValue) / 2
percent80 = (StopValue - StartValue) / 5 * 4
percent95 = (StopValue - StartValue) / 100 * 95
tStart = Timer
t1 = tStart
For StartValue = 1 To StopValue
'If statements below calculates/recalculates the time left of the loop
'If you need more or less calculationpoins you can just add or delete them
'The countdown starts when the script has reached 5%
If StartValue = CInt(percent5) Then
Start = 1 ' starts the countdown
t2 = Timer
timeleft = (t2 - tStart) / 5 * 95
End If
If StartValue = CInt(percent10) Then
t2 = Timer
timeleft = (t2 - tStart) / 10 * 90
End If
If StartValue = CInt(percent20) Then
t2 = Timer
timeleft = (t2 - tStart) / 20 * 80
End If
If StartValue = CInt(percent50) Then
t2 = Timer
timeleft = (t2 - tStart)
End If
If StartValue = CInt(percent80) Then
t2 = Timer
timeleft = (t2 - tStart) / 80 * 20
End If
If StartValue = CInt(percent95) Then
t2 = Timer
timeleft = (t2 - tStart) / 95 * 5
End If
If Start = 1 Then
t3 = Timer
timeleft = timeleft + (t1 - t3)
t1 = t3
End If
'devides the timeleft to minutes and seconds
mins = Fix(timeleft / 60)
secs = CInt(timeleft) - (mins * 60)
If secs < 10 Then
secs = "0" & secs
End If
'Prints the % done and estimated time left of the loop
Application.StatusBar = "Progress: " & Format(StartValue / StopValue, "Percent") & " Estimated time left " & mins & ":" & secs
'Dummyloop below to waste time, replace it with your code
MyTimer = Timer
Do
Loop While Timer - MyTimer < 0.03
Next StartValue
Application.StatusBar = False 'removes the statusbar
End Sub
|