The VBA DateSerial Function returns a Date from a supplied year, month and day number.
The syntax of the function is:
Where the function arguments are:
Year | - |
An integer representing the year. Note that:
|
|||||||||||||||
Month | - |
An integer representing the month. Integer values less than 1 or greater than 12 are interpreted as follows:
|
|||||||||||||||
Day | - |
An integer representing the day of the month. Integer values less than 1 or greater than the number of days in the current month are interpreted as follows:
|
' Two different ways to return the date "12/31/2015"
Dim dt1 As Date
Dim dt2 As Date
dt1 = DateSerial( 2015, 12, 31 )
' The variables dt1 and dt1 now both are equal to the Date 12/31/2015.
dt2 = DateSerial( 15, 12, 31 ) |
After running the above VBA code, the variables dt1 and dt2 are both equal to the date 12/31/2015.
Note that the two-digit Year number 15 has been interpreted as the year 2015.
The following VBA code shows how month numbers that are less than 1 or greater than 12 are interpreted as months in the previous or following year.
' Return Four Different Dates
Dim dt1 As Date
Dim dt2 As Date Dim dt3 As Date Dim dt4 As Date
dt1 = DateSerial( 2015, 0, 31 )
' dt1 is now equal to the Date 12/31/2014.
dt2 = DateSerial( 2015, 1, 31 )
' dt2 is now equal to the Date 1/31/2015.
dt3 = DateSerial( 2015, 12, 31 )
dt4 = DateSerial( 2015, 13, 31 )' dt3 is now equal to the Date 12/31/2015. ' dt4 is now equal to the Date 1/31/2016. |
After running the above VBA code, dt1 = 12/31/2014, dt2 = 1/31/2015, dt3 = 12/31/2015 and dt4 = 1/31/2016.
The following VBA code shows how day numbers that are less than 1 or greater than number of days in the current month are interpreted as days in the previous or following month.
' Return Four Different Dates
Dim dt1 As Date
Dim dt2 As Date Dim dt3 As Date Dim dt4 As Date
dt1 = DateSerial( 2015, 12, 0 )
' dt1 is now equal to the Date 11/30/2015.
dt2 = DateSerial( 2015, 12, 1 )
' dt2 is now equal to the Date 12/1/2015.
dt3 = DateSerial( 2015, 12, 31 )
dt4 = DateSerial( 2015, 12, 32 )' dt3 is now equal to the Date 12/31/2015. ' dt4 is now equal to the Date 1/1/2016. |
After running the above VBA code, dt1 = 11/30/2015, dt2 = 12/1/2015, dt3 = 12/31/2015 and dt4 = 1/1/2016.