VBA Year Function is a built-in function in MS Excel. It has one input argument or parameter. VBA Year Function returns the four-digit year from the specified Year. It is a ‘Data and Time’ type function. Default format of the year function is ‘yyyy’. The VBA Year Function can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA Year Function any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the Year function, where we can use this VBA Year Function and real-time examples.
- Objective
- Syntax of VBA Year Function
- Parameters or Arguments
- Where we can apply or use the VBA Year Function?
- Example 1: Display current system Year on the screen
- Example 2: Display current system Year on the Worksheet
- Example 3: Display last two digits of the Year using Format
- Download File
- Instructions to Run VBA Macro Code
- Other Useful Resources
VBA Year Function Syntax:
The syntax of the VBA Year function is
YEAR(date_value)
Parameters or Arguments:
There is one parameter or argument for the Year function in VBA. That is nothing but ‘date_value’. Where date_value is a date for which the year is to be returned.
There are no parameters or arguments for the Year function.
Where we can apply or use the VBA Year Function?
We can use this VBA Year Function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Display current year on the screen
Here is a simple example of the VBA Year Function. This below example macro uses the Year function and displays the four digit year from the specified date. In this case specified date is current date.
'Display Current Year on the screen Sub VBA_Year_Function_Ex1() 'Variable declaration Dim sCurrentYear As Integer 'Assign current Year to variable sCurrentYear = Year(Now) MsgBox sCurrentYear, vbInformation, "Current Year" End Sub
In the above example ‘sCurrentDate’ declared as a Integer data type. And Now is a vba function, it returns current system date and time. This variable ‘sCurrentDate’ now contains the four-digit year.
Output:Here is the screen shot of first example output.
Example 2: Display current year on the Worksheet
Here is another example of the VBA Year Function. This below example macro uses the Year function and displays the year from the specified date on the Worksheet named ‘VBAF1.Com’ in Range ‘B18’.
'Display Current Year on the Worksheet Sub VBA_Year_Function_Ex2() 'Variable declaration Dim dCurrentYear As Date 'Assign current Year to variable sCurrentYear = Now Sheets("VBAF1.com").Range("B18") = Year(sCurrentYear) 'or 'Sheets("Sheet1").Cells(18, 2) = Year(sCurrentYear) End Sub
Output:Here is the screen shot of second example output.
Example 3: Display last two digits of the Year using Format
Here is one more example with VBA Year Function. This below example macro uses the Date function and changes the format of the date. Finally, it displays the last two digits of the year from the specified date on the screen.
'Format and Display Current Year on the screen Sub VBA_Year_Function_Ex3() 'Variable declaration Dim sCurrentYear As Date 'Assign current Year to variable sCurrentYear = Date MsgBox Format(sCurrentYear, "yy"), vbInformation, "Current Year" End Sub
Output:Here is the screen shot of third example output.
Download File
Click on following link to download free example excel workbook to learn more about VBA Year function.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers