Saturday, October 11, 2008

Excel VBA TimeStamp – Milliseconds using Excel VBA

How to Get Time in Milliseconds using Excel VBA
The following function uses Timer function to get the milliseconds and append it to the current time
Public Function TimeInMS() As String
TimeInMS = Strings.Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." & Strings.Right(Strings.Format(Timer, "#0.00"), 2)
End Function
Timer function returns a Single representing the number of seconds elapsed since midnight.
Another method is to use API Functions as shown below
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Sub GetSystemTime Lib "kernel32" _
(lpSystemTime As SYSTEMTIME)
Public Function TimeToMillisecond() As String
Dim tSystem As SYSTEMTIME
Dim sRet
On Error Resume Next
GetSystemTime tSystem
sRet = Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & _
":" & tSystem.wMilliseconds
TimeToMillisecond = sRet
End Function
Millisecond timer using VBA, How to get milliseconds in VBA Now() function, VBA Now() function, VBA Timer function , Excel VBA Timer, VBA Milliseconds
See also:

4 comments:

  1. Ozwald Copperpot3:55 PM

    Be aware: Timer() does not work properly for milliseconds. In the general case, it's useful for recording seconds only.

    Timer() returns a Single which, put simply, can only store 8 digits of precision.

    At 12noon, the number of seconds is already 12x60x60 = 43,200. That's 6 digits already, so Timer() will only return 2 decimal places. So you might get tenths, maybe hundreds of a second.

    I'm looking for a better solution, will let you know if I find something.

    ReplyDelete
  2. Anonymous11:44 AM

    I wanted to generate files based on timestamp, I used c# 7 digit precision which was always unique, but the VB has milliseconds which is overwriting my files. May be there is some workaround :)

    ReplyDelete
  3. Anonymous6:15 PM

    Be aware that the value returned by GetSystemTime is only updated by Windows roughly every 15 milliseconds. If you frequently request this value, you might get results like:

    12:52:04.080
    12:52:04.080
    12:52:04.080
    12:52:04.080
    12:52:04.080
    12:52:04.096
    12:52:04.096
    12:52:04.096
    12:52:04.096
    12:52:04.096
    12:52:04.112
    12:52:04.112
    12:52:04.112
    12:52:04.112
    12:52:04.112
    12:52:04.127
    12:52:04.127
    12:52:04.127
    12:52:04.127
    12:52:04.127

    If you're ok with your times being approximately +/- 15 ms in accuracy, it's a simple function to use.

    ReplyDelete
  4. Anonymous3:07 AM

    You have tSystem which includes hour/minute/seconds. Why do you need to invoke Now? It will just add milliseconds to your time and hence reduce the accuracy of tSystem.wMilliseconds

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...

Visual Basic for Applications (VBA) Forum (recent threads)

CodeKeep VBA Feed

Visual Studio Tools for Office Forum (recent threads)

Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.

Office Business Applications (OBA) Team Blog

MSDN Code Gallery Published Resources For Tag VSTO

microsoft.public.vsnet.vstools.office Google Group