Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 544
Florian Avatar asked Jun 02 '09 12:06

Florian


People also ask

How do I use the DateDiff function in VBA?

Example #1 – To Find Differences in DaysStep 1: Create a macro name first. Step 2: Define Two Variables as Date. Step 3: Now, for the Date1 variable, assign “15-01-2018” and for the Date2 variable, assign “15-01-2019”.

Can you subtract dates in VBA?

Using the DateAdd function, we can add and subtract days, months, and years from the given date.

How do I calculate days between dates in Excel VBA?

The DateDiff function in Excel VBA can be used to get the number of days between two dates. Explanation: first, we declare two dates. Next, we initialize the two dates using the DateValue function. The DateDiff function has three arguments.


1 Answers

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).

like image 161
Adam Ralph Avatar answered Sep 28 '22 08:09

Adam Ralph