Finding Minutes & Seconds When Using Timer

S

StevenM

I’ve been using the Timer function to time my macros, but every now and then
I end up with a negative number when trying to calculate minutes and seconds.
For example:

Sub TestTimer()
Dim tTime As Single
Dim nMinutes As Long
Dim nSeconds As Long
tTime = Timer
nMinutes = CLng(tTime / 60)
nSeconds = CLng(tTime - (nMinutes * 60))
MsgBox "Timer = " & tTime & Chr(13) _
& nMinutes & " minutes and " & nSeconds & " seconds"
End Sub

One result which I’ve received was:
"Timer = 53818.06
897 minutes and –2 seconds"

897 minutes is 53820 seconds, just two more than what the Timer gave.

Why do I sometimes get a negative number?

Steven Craig Miller
 
M

macropod

Hi Steven,

Your code is rounding the number of minutes up. 53818.06 seconds is 896 minutes and 58 seconds. Try:
Sub TestTimer()
Dim tTime As Single
Dim nMinutes As Long
Dim nSeconds As Long
tTime = Timer
nMinutes = Int(tTime / 60)
nSeconds = tTime Mod 60
MsgBox "Timer = " & tTime & vbCrLf _
& nMinutes & " minutes and " & nSeconds & " seconds"
End Sub

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top