How to Use Datediff in Excel

By Jim Campbell

The "DateDiff" function in Excel lets you display the difference between two dates. The difference can be in days, years or months. The result shows in the cell where you type the equation into the Excel spreadsheet, and is displayed in an integer format next to the dates you want to evaluate.

Step 1

Click the Windows "Start" button and select "All Programs." Click "Microsoft Office," then click "Microsoft Excel" to start the program.

Step 2

Press the "Ctrl" and "O" keys on the computer. In the opened dialog window, double-click the file you want to edit. Click the cell where you want the results of the "DateDiff" function to appear.

Step 3

Click the "Developer" ribbon tab at the top of the Excel window. Click the "Visual Basic" button to open the VBA editor. Double-click "Sheet1" to open the editor to the current sheet.

Step 4

Type the following code in the editor:Function GetDifference (myDate1 as Date, myDate2 as Date) as Long GetDifference = DateDiff("d", myDate1, myDate2)End Function

Step 5

Click the "Save" button, then close the VBA editor. Type "=GetDifference(A2, A1)" and press "Enter." Replace "A2" and "A1" with the cells that contain the dates you want to compare. Excel calls the VBA function and displays the date difference in days.