DATEDIFF function in VBA is an inbuilt function in VBA which is also categorized under date and time function in VBA, this function is used to get the difference between two dates, this function takes three arguments the first argument is what part of the difference we want which can be year days or months or seconds and two dates and the result is an integer.

## DATEDIFF Function in VBA

DATEDIFF Function in VBA calculates the difference between in two dates in days, months, quarters, and years.

In excel finding the difference between two dates has many ways. You don’t need a special formula to calculate the difference between two dates.

For an example look at the below image.

If we want to calculate the difference between these two dates we can simply subtract the date 1 from date 2.

It has given us the difference between two dates in a number of days. This is what the problem with this generic formula. If we need the difference in months, years, quarters etc… it cannot give.

In this article, we will show you how to use this DateDiff function in VBA.

### What is the DATEDIFF Function in Excel VBA?

**DATEDIFF** in VBA stands for “Date Difference between two dates”.

This function can give us the number of the time interval between two dates. When we want to find the difference between two dates we can find it in days, weeks, months, quarters, etc..

To understand the function look at the below syntax of the function.

**Interval: **This nothing but in what way you want to calculate the date difference. Whether it is in days, months, weeks, quarters, etc… Below is the list of the same.

**Date 1: **What is the first date you want to find the difference.

4.6 (247 ratings) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion

**Date 2:** What is the second date you want to find the difference from Date 1.**Date 1: **What is the first date you want to find the difference.

Here formula is **Date 2 – Date 1.**

**[First Day of Week]: **What is the first day of the week? We can following arguments.

**[First Week Of the Year]: **What is the first week of the year. We can enter the following arguments.

### Examples of DATEDIFF Function in Excel VBA

The following are the examples of excel VBA DateDiff.

#### Example #1 – To Find Differences in Days

Assume you have two dates “15-01-2018” and “15-01-2019”. Let’s find all kinds of differences for these two dates.

**Step 1: **Create a macro name first.

**Code:**

Sub DateDiff_Example1() End Sub

**Step 2: **Define Two Variables as Date.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date End Sub

**Step 3: **Now for Date1 variable assign “15-01-2018” and for Date2 variable assign “15-01-2019”.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Date1 = "15-01-2018" Date2 = "15-01-2019" End Sub

**Step 4: **Now to store results define one more variable as Long.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" End Sub

**Step 5: **Now assign the value for this variable through DATEDIFF function in VBA

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result =DateDiff( End Sub

**Step 6: **First argument is what kind of difference we need between these two dates. Let’s say we need to find the number of days, so supply the argument as “D”.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result =DateDiff("D", End Sub

**Step 7: **What is the first date to find the difference. Our first date is “15-01-2018” that we have already assigned to the variable “Date1”. So supply the variable name here.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result =DateDiff("D",Date1, End Sub

**Step 8: **What is the second date to find the difference. The second date is “15-01-2019” which holds the value through variable “Date2”.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result = DateDiff("D", Date1, Date2) End Sub

**Step 9: **Ignore the last two parameters. Now assign the value of variable “Result” through the VBA message box.

**Code:**

Sub DateDiff_Example1() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result = DateDiff("D", Date1, Date2) MsgBox Result End Sub

Now run the code using F5 key or manually, we will get the difference between these two dates in a number of days.

So, from “15-01-2018” to 15-01-2019” the exact difference is 1 year, so we got 365 days as the result.

Like this, we can find the difference between two dates in time intervals.

#### Example #2 – To Find Difference in Months

**Code:**

Sub DateDiff_Example2() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result = DateDiff("M", Date1, Date2) MsgBox Result End Sub

** **

Run this code Using the F5 key or you can run manually to show the result as given below.

#### Example #3 – To Find Difference in Years

**Code:**

Sub DateDiff_Example3() Dim Date1 As Date Dim Date2 As Date Dim Result As Long Date1 = "15-01-2018" Date2 = "15-01-2019" Result = DateDiff("YYYY", Date1, Date2) MsgBox Result End Sub

Run this code using the F5 key or manually to see the result.

#### Assignment as a Practice

I hope you have understood the function of VBA DATEDIFF. Take look at the below homework for you. Find the difference between the below dates in “Months”.

If you have not found the way below is the readymade code for you.

**Code:**

Sub Assignment() Dim k As Long For k = 2 To 8 Cells(k, 3).Value = DateDiff("M", Cells(k, 1), Cells(k, 2)) Next k End Sub

You can run this code manually or press the F5 key to see the result.

### Recommended Articles

This has been a guide to VBA DateDiff Function. Here we learn how to use DateDiff Function to find differences in days, months, and years in Excel VBA along with practical examples and a downloadable template. Below you can find some useful excel VBA articles –

- 3 Courses
- 12 Hands-on Projects
- 43+ Hours
- Full Lifetime Access
- Certificate of Completion