Record time peroid to run code

  • Thread starter Thread starter gotroots
  • Start date Start date
G

gotroots

Hi

Is there a msgbox method that will record the length of time it take for
code to complete its execution.

Thank you.
 
Sub TestTime()
Dim StartTime As Single
StartTime = Timer

'do code here

MsgBox "Code took " & Timer - StartTime & " seconds"
End Sub
 
Hi,

Timer is the number of seconds since midnight so this will fail if the code
runs over midnight

Dim runtime As Long
Start = Timer
'Your code
MsgBox = Timer - Start


Mike
 
it would help if I read my code before posting it:(

Try this instead


Start = Timer
'Your code

MsgBox Timer - Start


Mike
 
Sub do_things()

StartTime = Timer

'do things

MsgBox Timer - StartTime

End Sub


Gord Dibben MS Excel MVP
 
Hi folks
Thanks for all the advice.

I went for Mike H. approach as it will deal with the midnight issue.

My test returned 1786.469

It would be helpful if the result can be in the hh:mm:ss format.

Thank you
 
In all the other replies, don't use Timer, use Time(). Then even if it takes
days, it will be accurate.
 
Try this:
MsgBox Format(Timer - Start,"HH:MM:SS")

Sometimes I want to check several substeps, in which case (just out of
habit) I tend to use:

Time1 = now()
'do code
Time2 = now()
'more code
Time3=now()
etc.

then just use any combo of those, such as
msgbox "Part 1: " & Format(Time2-Time1,"HH:MM:SS") & chr(13) & _
"Part 2: " & Format(Time3-Time2,"HH:MM:SS")

and so on.

HTH,
Keith
 
didn't do too much testing, but see if this works for you:

dim startTime as date
dim endTime as date
dim tTime as date
starttime = Now()
' your code
endTime = Now()
tTime = (endTime - startTime) * 86400
Debug.Print WorksheetFunction.Floor(tTime / 60, 1) & " minutes " & tTime Mod 60
& " seconds"

or you can use a msgbox instead of the debug.print statement

msgbox WorksheetFunction.Floor(tTime / 60, 1) & " minutes " & tTime Mod 60 & "
seconds"
 
Back
Top