Visual Basic for Applications VBA is integrated into the programs that come with the Microsoft Office suite, like Microsoft Word and Microsoft Excel. VBA lets you write macros short, simple programs , which can be used to automate a task you perform repetitively. For example, in Excel you can write a macro that lets you copy the data found in a range of cells from one column and paste it in another column. You can see how useful this is — especially if you use Excel for office work! Using Visual Basic for Applications requires a certain familiarity with the Visual Basic language. Visual Basic was developed by Microsoft to make it easy for developers to make Windows-friendly applications. Before we proceed, you may want to , to brush up the basics. The DateDiff function in VBA can be used to find out the time interval between two distinct specified time periods. You can change the interval parameter to day, weekday, week, hour, second or year among other values. The date1 and date2 will be starting date and the ending date of your two periods- and the interval you specify will be how you receive your result. Creating a Command Button Before we show you the code to write the DateDiff function, you should insert a command button in your Excel spreadsheet. What is a command button and why do we need one? A command button, when clicked, runs the code it was linked to. You can use command buttons to start the execution of a macro and to get results. In our case, we will link the command button to the DateDiff function. First, you should enable the developer tab in Excel. This tab is disabled by default. The developer lets you create macros and add features and designs to your spreadsheet. A new tab called Developer will now be available next to the View tab at the top. The Design Mode button at the top will become highlighted and your pointer will turn into a black cross. Click on any cell in the spreadsheet to spawn a command button, like in the screenshot given below: You can drag and drop the command button anywhere in the spreadsheet. You can change the name of the command button by right clicking on it and choosing properties. A large list of properties will pop up to the left. Renaming the Caption label will change the visiblename of your command button to something else. This will open up the Microsoft Visual Basic for Applications screen. This is where we write the code. Using DateDiff Function to Find Difference between Two Dates in Days Now, finally, we can write the code for the DateDiff function. Next, we need to assign values to thedateone and datetwo variables. Finally, we need to display the result. For this, we use a message box. The Design Mode will be active- click on the Design Mode button in the developer tab to deactivate it. Finally, click on the command button. A message box with the difference in dates should pop up. You can change the values of the dateone and datetwo variables to get different results. Using DateDiff Function to Find Difference between Two Dates in Weeks You can use the DateDiff function to find difference between two specified dates in number of weeks, instead of number of days. Why did that happen? In this case, the difference between the two dates was 0 in number of weeks. If you changed the value of dateone or datetwo be a month or two, you will get a different result. Try experimenting for yourself and see. You can also change the time interval to second s , minute n , hour h , day of year y or month y. For a list, select the DateDiff function in VBA with your mouse highlight it and press F1. You can try specifying the firstdayofweek and firstweekofyear parameters as well. The DateDiff function is one of the simplest and most useful functions in VBA. Feel free to experiment with it.