How to use the VBA DATEADD Function (Syntax + Example)

HomeVBA Functions LIST (Category Wise)How to use the VBA DATEADD Function (Syntax + Example)

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 into it. In simple words, it can add seconds, minutes, hours, days, months, or years to date or time value and returns 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 in which you want to add the specific number of intervals.

Example

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

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 the cell A1. Well, the date we have in the 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 has returned 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 which can’t be recognized as a date, VBA will return the run-time 13 error.

Related Functions

DATE | DATEDIFF | DATEPART | DATESERIAL | DATEVALUE | DAY | MONTH | MONTHNAME | WEEKDAY | WEEKDAYNAME | YEAR