The VBA Year Function returns the four digit year from a supplied date.
The syntax of the function is:
A simple example of the VBA Year function is shown below:
' Extract the year from the date "11/29/2015"
Const currDate = "11/29/2015"
Dim yearNum As Integer
yearNum = Year(currDate)
' The variable yearNum is now equal to 2015.
|
After running the above VBA code, the variable yearNum holds the value 2015.
If the Year function is supplied with a value that cannot be interpreted as a valid date, it will return the error: