Calculate time difference in milliseconds in VBA

Calculate time difference in milliseconds

Public Declare PtrSafe Sub Sleep Lib “kernel32” (ByVal milliseconds As Long)

Sub CalculateElapsedTimeInMilliseconds()

Dim startTime As Double
Dim endTime As Double
Dim elapsedT…


This content originally appeared on DEV Community and was authored by Wild Cat

Calculate time difference in milliseconds

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)

Sub CalculateElapsedTimeInMilliseconds()

    Dim startTime   As Double
    Dim endTime     As Double
    Dim elapsedTime As Double
    Dim message     As String

    startTime = Timer

    Call Sleep(1200) 'Wait 1.2 seconds

    endTime = Timer
    elapsedTime = endTime - startTime

    message = "Time "
    message = message & Format(Int((elapsedTime) / 60 / 60), "00") & ":"            'Hour
    message = message & Format(Int((elapsedTime) / 60) Mod 60, "00") & ":"          'Minute
    message = message & Format(Int(elapsedTime) Mod 60, "00") & "."                 'Second
    message = message & Format(Int((elapsedTime - Int(elapsedTime)) * 1000), "000") 'Millisecond

    Debug.Print message 'Time 00:00:01.200

End Sub

Calculate time difference in seconds

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As Long)

Sub CalculateElapsedTimeInSeconds()

    Dim startTime   As Date
    Dim endTime     As Date
    Dim elapsedTime As Date
    Dim message     As String

    startTime = Now

    Call Sleep(3000) 'Wait 3 seconds

    endTime = Now

    elapsedTime = endTime - startTime

    message = "Time " & Format(elapsedTime, "hh:mm:ss")

    Debug.Print message 'Time 00:00:03

End Sub


This content originally appeared on DEV Community and was authored by Wild Cat


Print Share Comment Cite Upload Translate Updates
APA

Wild Cat | Sciencx (2024-10-12T14:56:54+00:00) Calculate time difference in milliseconds in VBA. Retrieved from https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/

MLA
" » Calculate time difference in milliseconds in VBA." Wild Cat | Sciencx - Saturday October 12, 2024, https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/
HARVARD
Wild Cat | Sciencx Saturday October 12, 2024 » Calculate time difference in milliseconds in VBA., viewed ,<https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/>
VANCOUVER
Wild Cat | Sciencx - » Calculate time difference in milliseconds in VBA. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/
CHICAGO
" » Calculate time difference in milliseconds in VBA." Wild Cat | Sciencx - Accessed . https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/
IEEE
" » Calculate time difference in milliseconds in VBA." Wild Cat | Sciencx [Online]. Available: https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/. [Accessed: ]
rf:citation
» Calculate time difference in milliseconds in VBA | Wild Cat | Sciencx | https://www.scien.cx/2024/10/12/calculate-time-difference-in-milliseconds-in-vba/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.