VBA YEAR Function (Syntax + Example)

Last Updated: June 22, 2023
puneet-gogia-excel-champs

- Written by Puneet

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 a year and ignores the rest of the part. It’s just like the YEAR function in the worksheet.

Syntax

Year(Date)

Arguments

  • Date: A valid VBA date from which you want to get the year value.

Example

To practically understand how to use the VBA YEAR function, you need to go through the below example where we have written a vba code by using it:

example-vba-year-function
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 cell A1 and it has returned 2019 in the result as the date is 1-Jan-2019.

Notes

  • 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.