Excel

Show estimated time left in statusbar

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

Ago

Description:

Shows the estimated time left of the macro in the statusbar 

Discussion:

This can be used if you have a macro that takes a long time to finnish. Theres alot of diffrent progressbars out there but this one counts down the estimated time left. Now you know if you can leave the computer while the macro is running. The timer will start running when 5% is finnished. 

Code:

instructions for use

			

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

How to use:

  1. Open the Visual Basic Editor (VBE)
  2. Copy the code above (without first and last row)
  3. Paste it in your module
  4. Remove the dummy code at the bottom
  5. Replace the loop with your code
  6. Close VBE
  7. Run macro Estimated_time
  8. Look in bottom left corner (statusbar)
 

Test the code:

  1. Download the file
  2. Run the macro
 

Sample File:

Estimated_time.zip 9.26KB 

Approved by mdmackillop


This entry has been viewed 349 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express