Calculate Time Difference between two times in hours minutes seconds with VBA

In many situations with different begin and end timings we may wish to automate our calculations of the difference in hours, minutes and seconds. This can be done quickly and easily with Excel VBA.

Watch the training video:

Watch the vide on YouTube.

Here’s the complete VBA code to calculate time difference between two timings:

Sub timeDifference()
Dim lastrow As Long
lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
‘We calculate the total hours
Cells(i, 3) = DateDiff(“s”, Cells(i, 1), Cells(i, 2)) / (60 * 60)
‘here we get the integer value and ignore the fraction
Cells(i, 3) = Int(Cells(i, 3))
‘we use the fraction hours to calculate the minutes
Cells(i, 4) = DateDiff(“s”, Cells(i, 1), Cells(i, 2)) / 60 – Cells(i, 3) * 60
‘we get the integer value of the minutes
Cells(i, 4) = Int(Cells(i, 4))
‘we determine the fraction value of the minutes
Cells(i, 5) = DateDiff(“s”, Cells(i, 1), Cells(i, 2)) / 60 – Cells(i, 3) * 60 – Cells(i, 4)
‘ we convert the fraction minutes into seconds
Cells(i, 5) = Cells(i, 5) * 60
End Sub
Further reading:

Published by

Dinesh Kumar Takyar

Welcome to! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: For a structured Excel VBA training course online you can visit:

Leave a Reply

Your email address will not be published. Required fields are marked *