Return to VBA Code Examples

VBA DateAdd Function

DateAdd Description

The VBA DateAdd Function allows you to add (or subtract) days, months, years, hours, quarters, etc. to Dates or Times.

Simple DateAdd Examples

Here is a simple DateAdd example:

This code will add 20 days (indicated by “d”) to the date 4/1/2021:

vba dateadd function

Instead, we can change the Interval argument from “d” to “m” to add 20 months to the date 4/1/2021:

vba dateadd months

Instead of displaying the date in a message box, we can assign it to a variable:

DateAdd Syntax

In the VBA Editor, you can type  “DateAdd(” to see the syntax for the DateAdd Function:

vba dateadd syntax

The DateAdd function contains 3 arguments:

Interval: Time unit (Days, Months, Years, etc.). Enter as string. (ex. “m” for Month)

Setting Description
yyyy Year
q Quarter
m Month
y Day of Year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second

Number: Numeric value representing the number of time units to add. (ex. 20 to add 20 units)

Date: Initial Date. See next section.

Examples of Excel VBA DateAdd Function

Referencing Dates

To start, we will demonstrate different ways to reference dates using the VBA DateAdd Function.

Each of these DateAdd functions produce the same result:

Or you can reference a cell containing a date:

dateadd function cellvalue

Or create and reference a date variable:

Add or Subtract Dates

We have already shown you how to add to a date:

You can subtract from dates by using a negative number (ex. -20 instead of 20):

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

Adding Different Units of Time

Years

Quarter

Month

Day of Year

Day

Weekday

Week

Add to Today

These examples will add units of time to today using the Date Function.

 

Adding and Subtracting Time

The DateAdd function also works with Times.  Here are a few examples of adding (or subtracting) time to a time:

Hour

This example will add 2 hours to a time:

Minute

This example will subtract 120 minutes from the Current Time:

Second

VBA Programming | Code Generator does work for you!

Formatting Dates

When dates (or times) are displayed in Excel, UserForms, or Messageboxes, you should indicate how the dates should be displayed by using the Format Function.  We’ve included a few examples below:

 

vba dates