Millisecond capable timer in VBA

When debugging code in VBA, it can sometimes be useful to measure how long a particular action takes to complete. In-built functions such as Timer can return you a time as accurate as 1/64th of a second, but what if you need to compare times with a greater degree of accuracy?

The code you see below solves this problem by calling on an API function named ‘QueryPerformanceCounter’, which is capable of measuring times to a thousandth of a second. The code has been modified from the original snippet, which can be found here:
http://www.vbforums.com/showthread.php?545100-Time-Between-Events&p=3367886#post3367886

In order for the code to work, all you need to do is paste it into a normal code module.

Enjoy!

—–

Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" (t As Currency) As Boolean
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (t As Currency) As Boolean
Function GetTime() As Currency
 Static Freq As Currency
 Debug.Assert QueryPerformanceFrequency(Freq)
 QueryPerformanceCounter GetTime
 GetTime = GetTime / Freq
End Function
Sub CalcTime()
 Dim start As Currency, test1 As Currency, test2 As Currency
 Dim n As Long
 
 start = GetTime()
 '' First test code goes here:
 '' ---------------------------
 For n = 1 To 100000
 Cells(n, 1).Font.ColorIndex = 10
 Next
 '' ---------------------------
 test1 = GetTime() - start
 
 start = GetTime()
 '' Second test code goes here:
 '' ---------------------------
 Range("A1:A100000").Font.ColorIndex = 10
 '' ---------------------------
 test2 = GetTime() - start
 
 Debug.Print "Test 1: " & Format(test1, "0.000") & " seconds"
 Debug.Print "Test 2: " & Format(test2, "0.000") & " seconds"
End Sub