How to make a Macro estimate the amount of time it will take to run and keep track of elapsed time?

Sub Sub_Emperor15() Const NUMBER_OF_LOOPS As Long = 1000 Dim StartTime As Single Dim ElapsedTime As Single Dim RemainingTime As Single Dim i As Long StartTime = Timer() For i = 1 To NUMBER_OF_LOOPS ' do domething Debug.Print i * Cos(i) * Log(i) If i Mod 100 = 0 Then 'calculate time remaining every so often, and display it ElapsedTime = Timer() - StartTime RemainingTime = ElapsedTime / i * (NUMBER_OF_LOOPS - i) 'Application.StatusBar = "Loop=" & i & _ "; Time elapsed=" & Format(ElapsedTime, "#.0") & _ "; Time remaining= " & Format(RemainingTime, "#.0") Range("A1").Value = "Loop=" & i & _ "; Time elapsed=" & Format(ElapsedTime, "#.0") & _ "; Time remaining= " & Format(RemainingTime, "#.0") DoEvents End If Next i Application.StatusBar = "" Debug.Print: Debug.Print: Debug.Print Timer() - StartTime End Sub
This shows how to estimate remaining time and put it in a cell (or display it on the status bar--that code is commented out). On my machine, displaying in a cell is significantly faster.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.