Closed Thread
Results 1 to 9 of 9

Using Milliseconds in VBA

  1. #1
    Registered User
    Join Date
    07-15-2010
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    51

    Using Milliseconds in VBA

    How Do I use milliseconds in OnTIme function.

    VBA timer works in seconds, is there any fast way to get .1 sec or 0.05 sec....

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using Milliseconds in VBA

    Hello Slyone2,

    The Application.OnTime method is limited to 1 second minimum. To get finer timer resolution would require using an API timer. I would advise against it due to its instability in the Office environment.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-15-2010
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Using Milliseconds in VBA

    I have this from excel to measure calcs.....

    Please Login or Register  to view this content.
    Can it be modified for Appliation.OnTime

  4. #4
    Registered User
    Join Date
    07-15-2010
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Using Milliseconds in VBA

    Is that the same for Application.Wait ???

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using Milliseconds in VBA

    Hello Slyone2,

    You don't need to use the API calls. VBA returns the tick count through the Timer function. The fastest the Application.OnCall event can fire is every 1 second. The tick count will have increased by 1 second or 1000 milliseconds by the time the Application.OnTime event is fired.

    Here is how to measure a calculation...
    Please Login or Register  to view this content.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Milliseconds in VBA

    Timer and GetTickCount are different.

    Timer returns the number of seconds since midnight as a Single; on PCs, you get about 10ms precision.

    GetTickCount returns the number of milliseconds since restart as an unsigned 32-bit integer, which wraps after ~49 days.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    07-15-2010
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    51

    Re: Using Milliseconds in VBA

    On precision, on Xp 64 is it the same?
    And what about linux?

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using Milliseconds in VBA

    Hello Slyone2,

    I came across this timer for VBA that may work for you and it is free.

    Karl E. Peterson's VBA TimerObj

  9. #9
    Registered User
    Join Date
    03-30-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2003
    Posts
    1

    Thumbs up Using Milliseconds in VBA Ultimate solution

    'Put this on the Form w/ Command Button and text box...

    '*********************************************************************************
    Private Sub CommandButton1_Click()

    Dim StartTime As Long
    Dim NextFrameTime As Long

    timeBeginPeriod 1 'switch resolution to 1 ms
    Do
    StartTime = timeGetTime
    NextFrameTime = Interval + StartTime

    Text3.Text = Format(Now, "HH:nn:ss") & "." & Right(Format(Timer, "#0.00"), 2) '---> This will display time with milliseconds...

    DoEvents
    Do While timeGetTime < NextFrameTime
    DoEvents
    If timeGetTime + 5 < NextFrameTime Then
    Sleep NextFrameTime - (timeGetTime + 5)
    End If
    Loop
    Loop

    End Sub
    '*************************************************************************
    'Create a module and put the code below....
    '********************************************************************************
    Public Declare Function timeGetTime Lib "winmm.dll" () As Long
    Public Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
    '16-bit programs must use MMSYSTEM.DLL instead of WINMM.DLL

    Public Declare Sub Sleep Lib "kernel32" (ByVal uDuration As Long)
    '16-bit programs cannot do a "Sleep", just omit it in that case

    'If you found this code useful, please don't forget to thank me on my email --> Brix102778@gmail.com

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1