Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate elapsed time in seconds in VBA?

Tags:

datediff

vba

I have 2 strings, strStartTime and strEndTime.

strStartTime = "12:32:54" strEndTime = "12:33:05"

I want to find out how many seconds elapsed between strStartTime and strEndTime so I did this:

Dim dtDuration as date
dtDuration = DateDiff("s", CDate(strStartTime), CDate(strEndTime))  

The result I get is dtDuration = "#1/10/1900#" in the Locals watch window.

Why does this happen? How do I get dtDuration to equal 11 for the 11 seconds that elapsed between the start and end times?

like image 320
phan Avatar asked Feb 21 '12 21:02

phan


1 Answers

Just change variable type to Long:

Dim dtDuration as Long

VBA converts numerical results of DateDiff functions into variable with date type.

like image 117
Radek Avatar answered Nov 08 '22 03:11

Radek