The VBA YEAR function is listed under the date and time category of VBA functions. When you use it in a VBA code, it returns the year from the date which you supplied. As you know a valid VBA date is made up of the day, month, and year so it takes year and ignores the rest of the part. It’s just like the YEAR function in the worksheet.
- Date: A valid VBA date from which you want to get the year value.
To practically understand how to use VBA YEAR function, you need to go through the below example where we have written a vba code by using it:
Sub example_YEAR() Range("B1").Value = Year(Range("A1")) End Sub
In the above example, we have used YEAR to get the year from the date which we have in the cell A1 and it has returned 2019 in the result as the date is 1-Jan-2019.
- If the date specified is a value other than a date or a date that can’t be recognized as a date, VBA will return the run-time 13 error.
- And if the supplied date is NULL, it will return NULL.