Thank you for Visiting my Blog

Wednesday, 13 September 2017

How DateAdd functions works in VBA


DateAdd Function:
It 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 below arguments:
Part
Description
interval
Required. String expression that is the interval of time you want to add. Please refer below table .
number
Required. Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
date
Required. Variant (Date) or literal representing date to which the interval is added.
 
Below table represent how Interval can be used:

The interval argument has these settings:
Setting
Description
yyyy
Year
q
Quarter
m
Month
y
Day of year
d
Day
w
Weekday
ww
Week
h
Hour
n
Minute
s
Second


Remarks:

You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now.
To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").

Example:
This example takes a date and, using the DateAdd function, displays a corresponding date a specified number of months in the future.

 
Sub Adddt()
Dim FirstDate As Date    ' Declare variables.
Dim IntervalType As String
Dim Number As Integer
Dim Msg
IntervalType = "m"    ' "m" specifies months as interval.
FirstDate = InputBox("Enter a date")
Number = InputBox("Enter number of months to add")
Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)
 
End Sub

 

 

No comments:

Post a Comment