VBA DATEADD Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

The VBA DATEADD function is listed under the date and time category of VBA functions. When you use it in a VBA code, it returns a date or a time by adding an interval to it. In simple words, it can add seconds, minutes, hours, days, months, or years to date or time value and return that new date and time in the result.

Syntax

Dateadd(Interval, Number, Date)

Arguments

  • Interval: A string to specify the interval.
    • “D” – Days
    • “H” – Hours
    • “N” – Minutes
    • “M” – Months
    • “Q” – Quarters (of a Year)
    • “S” – Seconds
    • “ww” – Weeks
    • “yyyy” – Years
  • Number: The number of intervals to add to the date.
  • Date: The date on which you want to add the specific number of intervals.

Example

To practically understand how to use the VBA DATEADD function, you need to go through the below example where we have written a vba code by using it:

example-vba-dateadd-function
Sub example_DATE()
Range("B1") = DateAdd("YYYY", 2, Range("A1"))
End Sub

In the above example, we have used the DATEADD function to add three years to the date which we have in cell A1.

Well, the date we have in cell A1 is 13-Nov-2017, and in the function, we have used the interval “YYYY” to mention that we want to add a year to that and then we have specified the 2 as years.

As a result, it returned on 13-Nov-2019 which is the exact date after the 3 years.

Notes

  • If the value specified is a value other than a date or a date that can’t be recognized as a date, VBA will return the run-time 13 error.