What is VBA YEAR Function
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.
how to use it
To use VBA's YEAR function you need to understand its syntax and arguments:
- Date: A valid VBA date from which you want to get the year value.
Example to use VBA YEAR Function
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.
Below are some important points which you need to take care while using YEAR function in VBA.
- 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.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.