Timing

There are three ways you can do this: VBA.Timer, GetTickCount API or timeGetTime API.


VBA.Timer

This returns the number of seconds elapsed since midnight using a single-precision floating point value.
This is not a threaded control so may not be triggered at exactly the correct time.
Delays can be caused by other applications and or system processes.

Public starttime As Single 
Public timeelapsed As Single

Public Sub RunCode()
Dim l As Long
Dim a As Long
    For l = 1 To 800000000
        a = a + 1
    Next
End Sub

Public Sub Timer1()
    starttime = VBA.Timer
    Call RunCode
    timeelapsed = VBA.Timer - starttime
'Call MsgBox(timeelapsed & " seconds")
    Call MsgBox(Format(timeelapsed / 60, "##0.###") & " minutes")
End Sub

GetTickCount API

This returns the number of milliseconds that have elapsed since Windows was started.
This will run for 49 days before resetting back to zero.

Public Declare PtrSafe Function GetTickCount Lib "kernel32.dll" () As Long 

Public Sub Timer2()
    starttime = GetTickCount()
    Call RunCode
    timeelapsed = (GetTickCount() - starttime)
    Call MsgBox(timeelapsed / 1000 & " seconds")
    Call MsgBox(timeelapsed / 60000 & " minutes")
    Call MsgBox(timeelapsed / 3600000 & " hours")
End Sub

timeGetTime API

This returns the number of milliseconds that have elapsed since Windows was started.

Public Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long 

Public Sub Timer3()
    starttime = timeGetTime()
    Call RunCode
    timeelapsed = (timeGetTime() - starttime)
    Call MsgBox(timeelapsed & " seconds")
End Sub

Finding Bottlenecks

link - msdn.microsoft.com/en-us/library/aa730921 

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext