## Calculate Difference Between two Dates in Excel VBA

Calculate Difference Between two Dates in Excel VBA. Most of the time we wanted find differences of two dates. It’s a very common and regular excercise in Excel and VBA when dealing with dates. We can find this using Excel VBA **DateDiff** function. In order to calculate we need start date and end date. It returns an integer value as a result. We use Excel **DateDif** function in Excel to perform same task.

**Table of Contents:**

### Macro to Calculate Days Between two Dates in Excel VBA

Let us see the macro to calculate Difference Between two Dates in Excel VBA.

'Calculate Difference Between two Dates in Excel VBA 'Calculate Difference Between two Dates in Excel VBA Sub VBA_Calculate_Difference_Between_Two_Dates() 'Variable declaration Dim sDate As Date, eDate As Date Dim iValue As Integer 'Start Date sDate = "10-Jan-2020" 'End Date eDate = "25-May-2020" iValue = DateDiff("d", sDate, eDate) 'Display output on the screen MsgBox "The Difference Between two Dates in days : " & iValue, vbInformation, "Difference Between two Dates in Days" End Sub

Here is the output screenshot of above mcaro.

### Function to Get the Difference Between two Dates in Excel VBA

Here is the function to find difference between two days in Excel VBA. Where StartDate and EndDate are two dates to select dates from the range on the worksheet.

'Function to get Difference Between two Dates in Excel VBA Function VBA_Calculate_Difference_Between_Two_Dates(StartDate As Range, EndDate As Range) 'Calculate Difference Between two Dates in Excel VBA VBA_Calculate_Difference_Between_Two_Dates = DateDiff("d", StartDate, EndDate) End Function

**Output:**Here is the screenshot for your reference for the above function.

### Instructions to use Macro

Here are the instructions to use above macro in Visual basic editor.

- Open Visual Basic Editor(VBE) by clicking Alt +F11
- Goto code window by clicking F7
- Copy above specified macro or procedure
- Paste above copied code in code window
- Run macro by clicking F5 or Run command
- You can see ouput on the screen
- Find above specified output screenshot.

### Related VBA Functions used in this article

We have used Date and DateDiff VBA functions. Click on below specified links to learn more about VBA functions.

SebastianHi

Is it possible to run it as a function instead of msg. box so it can be used on several dates?

SomeswariHi Sebastian,

As requested, we have created function in the above tutorial for your reference.

Hope this helps!