What is VBA DATEADD Function
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.
how to use it
To use VBA's DATEADD function you need to understand its syntax and arguments:
Dateadd(Interval, Number, Date)
- 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 use DATEADD Function in VBA
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.
Below are some important points which you need to take care while using DATEADD function in VBA.
- 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.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.