ProjectX
ProjectX

Reputation: 21

VBA: Need to calculate time difference between times from different dates

I need help with calculating time difference between times from two different dates.

Cell(1,1) contains: 19/06/2016 01:00:00 
Cell(1,2) contains: 20/06/2016 02:30:00 

The answer should be: 25:30:00 Following is my code:

Dim a As Date, b As Date    
a = Cells(1, 1).Value
b = Cells(1, 2).Value
Cells(1, 3).Value = TimeValue(b) - TimeValue(a)

But this code gives me 1:30:00

Upvotes: 1

Views: 4157

Answers (1)

Gary's Student
Gary's Student

Reputation: 96791

Just subtract:

Sub luxation()
    Dim a As Date, b As Date
    a = Cells(1, 1).Value
    b = Cells(1, 2).Value
    Cells(1, 3).Value = b - a
    Cells(1, 3).NumberFormat = "[hh]:mm:ss"
End Sub

enter image description here

Upvotes: 4

Related Questions