DateAdd Function in VBA

How to manipulate dates using the DateAdd function in Excel VBA.

DateAdd Function in VBA

DateAdd function

Returns a Variant (Date) containing a date to which a specified time interval has been added.

Syntax
DateAdd(interval, number, date)

The DateAdd function syntax has the following named arguments:

ItemDescription
intervalThis argument is required. It is a string expression that is the interval of time you want to add.
numberThis argument is required. It is a numeric expression that is the number of intervals you want to add. It can be positive or negative. We use a positive number to get dates in the future. Negative numbers are used to get dates in the past.
dateThis argument is required. It is a Variant (Date) data type or literal. It represents the date to which the interval is added.
DateAdd Function Arguments

The macro code below shows a few examples of dateadd function. Only one line is active and the other lines have been commented out using an apostrophe:

Private Sub CommandButton1_Click()
Me.TextBox2.Value = DateAdd(“yyyy”, 5, Me.TextBox1.Value)
‘Me.TextBox2.Value = DateAdd(“m”, 2, Me.TextBox1.Value)
‘Me.TextBox2.Value = DateAdd(“d”, 2, Me.TextBox1.Value)
End Sub

From the above VBA code we can easily see that years, months or days can be added to our date.

The interval argument has these terms:

TermDescription
yyyyYear
qQuarter
mMonth
yDay of year
dDay
wWeekday
wwWeek
hHour
nMinute
sSecond
Terms used in the interval argument

Further reading:

DateAdd Function in VBA

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.