Excel VBA Year Function

Excel VBA Year Function: The Year function in VBA returns the four-digit year for the specified date.

Syntax

Year (Date)

Parameter

Date (required) – This parameter represents the significant date.

Return

This function returns the four-digit year for the specified date.

Example 1

Sub YearFunction_Example1()
 ' Extracting the year from the specified date
 Dim currDate As Date
 currDate = "02/29/2020" 
 Dim year_val As Integer
 year_val = Year(currDate)
 ' The variable year_val will return 2020. 
 Cells(1, 1).Value = year_val
 End Sub 

Output

2020

VBA Year Function

Example 2

Sub YearFunction_Example2()
 ' Extracting the year from the specified date
 Dim year_val As Integer
 'it will take today's date
 year_val = Year(Date)
 ' The variable year_val will return 2020.
 Cells(1, 1).Value = year_val
 End Sub 

Output

2020

VBA Year Function

Example 3

Sub YearFunction_Example3()
 ' Extracting the year from the specified date
 Dim currDate
 currDate = 0
 Dim year_val As Integer 
 year_val = Year(currDate)
 ' The variable year_val will return 1899. 
 Cells(1, 1).Value = year_val 
 End Sub 

Output

1899

VBA Year Function