I was looking for a timer function that easily can be embedded in my coeds to show me how many seconds each step is taking. I am doing this with a regular debug.print and several variables which made my code busy and also hard to do subtracting new timer from last recorded step was redundant as well. I am looking for a function to capture the time takes for each step and also finding out the total elapsed time from the first step.
Here is what I developed for that purpose. You have to declare a public object of "Scripting.Dictionary" type and also a long variable as ti.
Public d As Object, ti as long
and in one you your procedures, prior to calling the function:
Set d = CreateObject("Scripting.Dictionary")
You can use early binding if you have Microsoft Scripting Runtime already added to your library. Your declaration would be like below:
Dim d As New Scripting.Dictionary, ti as long
anyway here is the:
Title is a string as a reminder or tag of the stage/step in the code.
Newstart should set TRUE, for the first instance of the function.
NeedTotal should set true if you want total elapsed time up until that stage.
Public Function gettimer(Optional ByVal Title As String, _
Optional ByVal Newstart As Boolean, _
Optional ByVal NeedTotal As Boolean)
If Newstart = True Or ti = 0 Then
ti = 0
d("T0") = Timer
gettimer = ti & vbTab & vbTab & "0.00" & vbTab & vbTab & "Start @ " _
& Format(Time, "h:mm:ss") & vbTab & Title
Else
gettimer = ti & vbTab & vbTab & Format(Round(Timer - d("T" & ti - 1), 2), "0.00") _
& vbTab & vbTab & Title
d("T" & ti) = Timer
End If
If NeedTotal Then
gettimer = gettimer & vbTab & vbTab & vbTab & "Time elapsed from the start = " _
& vbTab & Round(d("T" & ti) - d("T0"), 4)
End If
ti = ti + 1
Debug.Print gettimer
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.