Elapsed time - milisecond

Starbucks_33

Active Member
Joined
Jun 16, 2008
Messages
345
Hi,

I wanted to get the elapsed time for running my program. The problem was that the smallest units of time I could do was seconds. Is there a way to display how many mili seconds have elapsed?

To get the seconds I was just using.


Time_Run = Now
.... executes code
TimeElapsed = (60 * Minute(Now - Time_Run) + Second(Now - Time_Run))

But I wanted to get hundreths of a second?

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This will give you seconds...
Code:
Sub TimeCheck()
'
    Start = Timer
    'your code to run here
    MsgBox "Process took " & Timer - Start & " Seconds"
End Sub
 
Upvote 0
THats the code I currently use, but my question was can you get time in finer units the seconds?

I wanted hundreths or thousandths of a second.

Is there any way to do this?
 
Upvote 0
Try formating the cell your time value goes in:
Code:
Sub TimeCheck()
    Start = Timer
    '.... execute code here"
    Range("K4").Value = Timer - Start
    Range("K4").NumberFormat = "0.####"
End Sub
 
Upvote 0
Hi

You can use an API function to get this (it returns the time elapsed in milliseconds since Windows was started):

Code:
Declare Function GetTickCount Lib "kernel32" () As Long

Sub test()

Dim i As Long, j As Long

i = GetTickCount

Application.Wait Now + TimeSerial(0, 0, 1)

j = GetTickCount

MsgBox "time elapsed = " & (j - i) / 1000 & " seconds"

End Sub
 
Upvote 0
For hundredths of second you can use the worksheet function Now() instead of the vba one:


Code:
Sub Test()
Dim tStart As Double
 
tStart = [now()]
 
' some code
 
MsgBox Application.WorksheetFunction.Text([now()] - tStart, "h:mm:ss.00")
 
End Sub
 
Upvote 0
For hundredths of second you can use the worksheet function Now() instead of the vba one:


Code:
Sub Test()
Dim tStart As Double
 
tStart = [now()]
 
' some code
 
MsgBox Application.WorksheetFunction.Text([now()] - tStart, "h:mm:ss.00")
 
End Sub
I've tried this using Excel 2003 - it doesn't work. The milliseconds are still truncating to 000.
 
Upvote 0
I find that this level of measurement leaves a lot to be desired because the results are subject to other multi-tasking activities, including system interrupts to carry out 'system critical' tasks. Further, even the system timer does not support millisecond accuracy -- the documentation for GetTickCount states its accuracy is somewhere in the range of 10 to 16 milliseconds.

If you are interested check out the 'standard' high resolution timer. I search google whenever I want to look it up. This time around I found http://msdn.microsoft.com/en-us/library/aa964692(VS.80).aspx

THats the code I currently use, but my question was can you get time in finer units the seconds?

I wanted hundreths or thousandths of a second.

Is there any way to do this?
 
Upvote 0

Forum statistics

Threads
1,223,604
Messages
6,173,315
Members
452,510
Latest member
RCan29

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