Your Programming and Traning Professionals

Excel DateAdd Function: Add or Subtract Days from a Date


The Excel DateAdd function is used to perform some interesting date arithmetic. You can use it for example to add or subtract days from a date you are working with.

The Basics:

DateAdd(
interval, number, date)

Interval= The frequency type that you want to work with. For example if we want to work with years we would use the ‘yyyy’ attribute. If we want to work with weeks we would use the ‘ww’ attribute.

Number = The values that we want to add or substract

Date = The date that we are working with

Now let’s look at an example. Let’s assume that we have a date value of 1/1/2010 and we want look at the following scenarios, namely:


1)What is the date if we add a year


2)What is the date if we subtract a week


3)What is the value if we added 25 days


 

 

Code

Dim DateToProcess As Date’Variable to hold the date value

DateToProcess = ActiveSheet.Cells(2, 1).Value’Assign the date value to our variable

‘Now let’s add a year

ActiveSheet.Cells(6, 1).Value = DateAdd(“yyyy”, 1, DateToProcess)

‘Subtract a week

ActiveSheet.Cells(8, 1).Value = DateAdd(“ww”, -1, DateToProcess)

‘Now let’s add a year

ActiveSheet.Cells(10, 1).Value = DateAdd(“d”, 25, DateToProcess)

Output