PC Review


Reply
Thread Tools Rate Thread

Grab Time with milliseconds included in VBA

 
 
Erick
Guest
Posts: n/a
 
      26th Oct 2004
I read one post that had a formula like "=TEXT(NOW(),"SS.00")", that
works, but I can not find anyway to use VBA to include milliseconds in
the time. I have tried worksheets("sheet1").cells(1,1) =
format(Time,"SS.00") and simular code but still can not get vba to
give me the milliseconds.


Thank you,
EKlassen
 
Reply With Quote
 
 
 
 
David Adamson
Guest
Posts: n/a
 
      26th Oct 2004
Search for Sleep

The following

'calls the sleep function and delays macro by 1/2 second
Sleep 500


"Erick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I read one post that had a formula like "=TEXT(NOW(),"SS.00")", that
> works, but I can not find anyway to use VBA to include milliseconds in
> the time. I have tried worksheets("sheet1").cells(1,1) =
> format(Time,"SS.00") and simular code but still can not get vba to
> give me the milliseconds.
>
>
> Thank you,
> EKlassen



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      26th Oct 2004
Hi Erick,

If you are trying to return the time like a stopwatch you could use the
following API which returns the time in milliseconds since system startup:

Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
Sub testTimer()
Dim t As Long
t = GetTickCount

For i = 1 To 1000000
a = a + 1
Next

MsgBox GetTickCount - t, , "Milliseconds"
End Sub

Regards,
Peter

"Erick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I read one post that had a formula like "=TEXT(NOW(),"SS.00")", that
> works, but I can not find anyway to use VBA to include milliseconds in
> the time. I have tried worksheets("sheet1").cells(1,1) =
> format(Time,"SS.00") and simular code but still can not get vba to
> give me the milliseconds.
>
>
> Thank you,
> EKlassen



 
Reply With Quote
 
Erick
Guest
Posts: n/a
 
      28th Oct 2004
What I am looking for is to get the exact Date and Time down to the
Millisecond when I press a Shortcut Key. Peter, I am not exactly sure
what your code is telling me. It looks like it is timing how long it
takes your code to execute? Every time I run it I get a number in the
msgbox like "1,206,000". Is this supposed to be converting it into
milliseconds with the msgbox? Hmmm?

I decided to use =TEXT(NOW(),"hhmmss.00" formula and refreshing it,
grabbing the value and then putting it where I want it. It seems to
work ok like that. It might not be very accurate? Still more
accurate than me with a stopwatch.

Thank you for the code,
Eklassen



"Peter T" <peter_t@discussions> wrote in message news:<(E-Mail Removed)>...
> Hi Erick,
>
> If you are trying to return the time like a stopwatch you could use the
> following API which returns the time in milliseconds since system startup:
>
> Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
> Sub testTimer()
> Dim t As Long
> t = GetTickCount
>
> For i = 1 To 1000000
> a = a + 1
> Next
>
> MsgBox GetTickCount - t, , "Milliseconds"
> End Sub
>
> Regards,
> Peter

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      28th Oct 2004
Hi Erick,

>Peter, I am not exactly sure
> what your code is telling me.


It should be telling you the time in milliseconds (ms) to run the sample
code, namely the difference in time (ms) between the two calls to the API. I
find this useful for "relative" time comparisons though I'm not sure how
accurate it is purly in terms of ms.

See "High Resolution Timers" at Decision Modals:
http://www.decisionmodels.com/downloads.htm

Also see the VBA's 'Timer' function without using an API. Help says it
records seconds since midnight but it appears to return an extra two decimal
places.

> What I am looking for is to get the exact Date and Time down to the
> Millisecond when I press a Shortcut Key


I'm not sure a time in ms activated by a human key press would be
meaningful, assuming you have found some way to accurately set the time to
1ms, your system clock keeps accurate time (mine loses 1 min / month) and
the DateTime clock records ms (it doesn't).

Regards,
Peter

"Erick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What I am looking for is to get the exact Date and Time down to the
> Millisecond when I press a Shortcut Key. Peter, I am not exactly sure
> what your code is telling me. It looks like it is timing how long it
> takes your code to execute? Every time I run it I get a number in the
> msgbox like "1,206,000". Is this supposed to be converting it into
> milliseconds with the msgbox? Hmmm?
>
> I decided to use =TEXT(NOW(),"hhmmss.00" formula and refreshing it,
> grabbing the value and then putting it where I want it. It seems to
> work ok like that. It might not be very accurate? Still more
> accurate than me with a stopwatch.
>
> Thank you for the code,
> Eklassen
>
>
>
> "Peter T" <peter_t@discussions> wrote in message

news:<(E-Mail Removed)>...
> > Hi Erick,
> >
> > If you are trying to return the time like a stopwatch you could use the
> > following API which returns the time in milliseconds since system

startup:
> >
> > Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
> > Sub testTimer()
> > Dim t As Long
> > t = GetTickCount
> >
> > For i = 1 To 1000000
> > a = a + 1
> > Next
> >
> > MsgBox GetTickCount - t, , "Milliseconds"
> > End Sub
> >
> > Regards,
> > Peter



 
Reply With Quote
 
 
 
Reply

« Excel VB help | * »
Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Time milliseconds format Kaykayme Microsoft Excel Programming 6 29th Oct 2008 06:44 PM
Can time be measured to milliseconds? =?Utf-8?B?UGZsdWdz?= Microsoft Excel Programming 2 22nd Jun 2006 01:40 AM
Getting the time down to milliseconds =?Utf-8?B?TmV2aWUgYW5kIFBoaWw=?= Microsoft Access Form Coding 7 2nd Jan 2005 11:21 PM
Current Time in milliseconds Oz Microsoft Dot NET Compact Framework 3 18th May 2004 04:37 PM
system time in milliseconds sarab Microsoft Windows 2000 Developer 1 1st Apr 2004 03:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:14 AM.