Using DatedDif Function in Excel VBA

We can calculate the difference between two date values like difference in years, months, days, quarters, weeks, hours, minutes and seconds. This can be useful in a variety of situations like inventory management. Here we might wish to know how many months or days an item was on the shelf before being sold.

Watch the training video below:

Watch this video on YouTube.

Here’s the complete VBA code to automate the difference calculations between two date values:

Sub dateDifference()
Cells(2, 3) = DateDiff(“yyyy”, Cells(2, 1), Cells(2, 2))
Cells(2, 4) = DateDiff(“m”, Cells(2, 1), Cells(2, 2))
Cells(2, 5) = DateDiff(“d”, Cells(2, 1), Cells(2, 2))
Cells(2, 9) = DateDiff(“q”, Cells(2, 1), Cells(2, 2))
Cells(2, 10) = DateDiff(“w”, Cells(2, 1), Cells(2, 2))
Cells(2, 11) = DateDiff(“h”, Cells(2, 1), Cells(2, 2))
Cells(2, 12) = DateDiff(“n”, Cells(2, 1), Cells(2, 2))
Cells(2, 13) = DateDiff(“s”, Cells(2, 1), Cells(2, 2))
End Sub
Further reading:

Excel 2013 Power Programming with VBA

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:

2 thoughts on “Using DatedDif Function in Excel VBA”

Leave a Reply

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