The basic and classic and most popular mode to time your code is achieved through Timer function which is natively supplied by VBA. It returns a Single representing the number of seconds elapsed since midnight. Hence, your code will look like following when you want to measure the time and it gives the result in Seconds.

Dim StartTime As Single, TimeElapsed As Single
StartTime = Timer
< --- Your Code Follows --->
TimeElapsed = Timer - StartTime

How Accurate is Timer Function

It returns the result in a Single data type variable. A Single data type variable can hold a maximum of 8 digits including the decimal. Hence, if you look at the result of Timer function at 3.49 PM, its result would beĀ 56964.29. Its result at 00:50 AM would be 3006.773 and its result at 11:51 PM would be 85850.41. Hence, it has a minimum accuracy of two decimal points which is sufficient for most of our tasks.

The system clock tick precision is 1/64 of a second or 1/256 of a second. Various opinions exist on this front and there is no right Microsoft Documentation on this which acts as authentic information. This much precision should be OK for most of our jobs.

Overcome MIDNIGHT Problem of TIMER Function

Since Timer results the number of seconds which have passed since midnight, hence if your code is still running when next midnight is reached, it will not give right results. Hence, if you started your code @ 11.54 pm, the Timer function would give you the result of 57255.91. At midnight, the Timer function resets to 0 as it counts the number of seconds past midnight only. If you job finishes at 00:01 AM, it means that new Timer value is 60 and hence your Time Elapsed would be negative which is not obviously right.

To overcome this problem, we need to check the date part also and modify the Timer code as below -

Dim StartTime As Single, TimeElapsed As Single
Dim StartDate As Date
StartDate = Date
StartTime = Timer
' < --- Your Code Follows --->
TimeElapsed = 86400 * (Date - StartDate) + Timer - StartTime