Calculate remaining time of macro with average

miicker

Board Regular
Joined
Jun 1, 2014
Messages
75
Hello everyone,

I have a macro which can run a long time. I have build in code to estimate the time remaining. I did it like this:
Code:
Dim StartTime As DoubleDim SecondsElapsed As Double
RemainingTestcases = Range("TotalTestCases").Value

'Code within while loop:
'Determine how many seconds code took to run
SecondsElapsed = Round(Timer - StartTime, 2)

RemainingTestcases = RemainingTestcases - 1
MsgBox "Time remaining: " & RemainingTestcases * SecondsElapsed & " Seconds ", vbInformation

So what this does is reset the timer every loop, and than multiple the time the code ran that loop by the number of testcases remaining.
This works good and gives a clear image of estimated time remaining, but when I run for example 800 testcases, which all take about 2-3 seconds per testcase, the time remaining variates from 1600-2400 seconds. I would like to solve this 'problem' by using the average time, so after the first loop, the average time is for example 2,4 seconds (the time the first loop took), after the second loop (2 seconds) the average time is 2,2 seconds, after the third loop (3 seconds) the average time is 2,47 seconds. This way I'm hoping for smaller differences each time the remaining time is calculated.

I have no idea on how to do this, so i'm hoping someone here can help me.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
try this ( untested)
Code:
Dim StartTime As Double
Dim SecondsElapsed As Double
Dim totalsec As Long
Dim cnt As Long
Dim avesec As Long


RemainingTestcases = Range("TotalTestCases").Value
cnt = 0
totalsec = 0


'Code within while loop:
'Determine how many seconds code took to run
SecondsElapsed = Round(Timer - StartTime, 2)
cnt = cnt + 1
totalsec = totalsec + SecondsElapsed
avesec = totalsec / cnt
RemainingTestcases = RemainingTestcases - 1
MsgBox "Time remaining: " & RemainingTestcases * avesec & " Seconds ", vbInformation
 
Upvote 0
Thanks for the help, minutes before you posted this I thought of this solution, just testing it, and it works fine, thanks!
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,667
Members
449,462
Latest member
Chislobog

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