How to get a DateDiff-Value in milliseconds in VBA (Excel)?

calendar_today Asked Jun 2, 2009
thumb_up 31 upvotes
history Updated April 16, 2026

Question posted 2009 · +14 upvotes

I need to calculate the difference between two timestamps in milliseconds. Unfortunately, the DateDiff-function of VBA does not offer this precision. Are there any workarounds?

Accepted answer +31 upvotes

You could use the method described here as follows:-

Create a new class module called StopWatch Put the following code in the StopWatch class module:

Private mlngStart As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Sub StartTimer()
    mlngStart = GetTickCount
End Sub

Public Function EndTimer() As Long
    EndTimer = (GetTickCount - mlngStart)
End Function

You use the code as follows:

Dim sw as StopWatch
Set sw = New StopWatch
sw.StartTimer

' Do whatever you want to time here

Debug.Print "That took: " & sw.EndTimer & "milliseconds"

Other methods describe use of the VBA Timer function but this is only accurate to one hundredth of a second (centisecond).

2 code variants in this answer

  • Variant 1 — 10 lines, starts with Private mlngStart As Long
  • Variant 2 — 7 lines, starts with Dim sw as StopWatch

Top excel-vba Q&A (6)

+31 upvotes ranks this answer #7 out of 136 excel-vba solutions on this site — top 5%.