Get Year in VBA: Date Functions Explained

VBA & macros · Updated November 23, 2026

To get the year from a date in VBA, use the Year function: Year(Date) returns the current year as an integer, and Year(dateValue) returns the year from any date variable or expression. For example, Year(#2024-03-15#) returns 2024. This function is part of VBA’s date/time library and works in all Office applications that support VBA, including Excel, Access, and Word.

Understanding the Year Function

The Year function takes a single argument that must be a valid date or a string that can be interpreted as a date. It returns an integer between 100 and 9999, representing the year of the given date. The function uses the locale settings of the system to interpret date strings, so be cautious with formats like “03/04/2024” which could be March 4 or April 3 depending on regional settings.

Here is a simple example in a VBA subroutine:

Sub ShowYear()
    Dim myDate As Date
    Dim yr As Integer
    myDate = #2024-07-19#
    yr = Year(myDate)
    MsgBox "The year is " & yr
End Sub

This macro displays a message box with the year 2024. The Year function is straightforward, but it is often used in combination with other date functions like Month and Day to extract complete date components.

Using Year with Current Date

To get the current year, call Year(Date). The Date function returns today’s date as a Date type. This is useful for age calculations, fiscal year reporting, or dynamic file naming. For example:

Sub CurrentYearExample()
    Dim currentYear As Integer
    currentYear = Year(Date)
    Debug.Print "Current year: " & currentYear
End Sub

In the Immediate window, this prints the current year (e.g., 2025). Note that Year returns an integer, so you can use it in arithmetic directly, like calculating a person’s age: Year(Date) - Year(birthDate).

Extracting Year from a Cell in Excel

In Excel VBA, you often need to read a date from a worksheet cell and extract its year. Use the Range object to get the value, then pass it to Year. Here’s an example:

Sub YearFromCell()
    Dim cellDate As Date
    Dim yr As Integer
    ' Assumes A1 contains a date
    cellDate = Range("A1").Value
    yr = Year(cellDate)
    MsgBox "Year in A1: " & yr
End Sub

If the cell contains a date, .Value returns a Date type. If it contains a string that looks like a date, VBA will try to convert it. If the cell is empty, Year will return 1899 (the base year for Excel dates), which is a common pitfall. Always check for empty cells or use IsDate to validate.

Common Pitfalls and Errors

One frequent mistake is passing a date string without proper conversion. For example, Year("2024-03-15") works because VBA converts the string to a date, but Year("March 15, 2024") might fail in non-English locales. Another issue is using Year on a cell that contains a time-only value, which returns 1899 because Excel treats times as fractions of a day starting from January 0, 1900.

To avoid errors, use IsDate to check if a value can be interpreted as a date before calling Year. For example:

Sub SafeYear()
    Dim v As Variant
    v = Range("A1").Value
    If IsDate(v) Then
        MsgBox Year(v)
    Else
        MsgBox "Not a date"
    End If
End Sub

Also, remember that VBA’s date system starts at December 30, 1899, so any date before that will cause an overflow error. In practice, you’ll rarely encounter such old dates, but be aware if you work with historical data.

Formatting Year as Text

Sometimes you need the year as a string, for example to build a filename like “Report_2024.xlsx”. You can use the Format function:

Sub FormatYear()
    Dim yearText As String
    yearText = Format(Date, "yyyy")
    MsgBox "Year as text: " & yearText
End Sub

This returns a four-digit year as a string. If you need a two-digit year, use “yy” but be aware of the Y2K issue: two-digit years are ambiguous. For most applications, use four digits.

Year in Worksheet Formulas vs. VBA

In Excel, you can also use the YEAR worksheet function directly in a cell, like =YEAR(A1). In VBA, you have two options: use the VBA Year function or use Application.WorksheetFunction.Year. The latter is rarely needed because VBA’s built-in function is sufficient. However, if you are working with arrays or need to evaluate a formula, you might use the worksheet function:

Sub UseWorksheetFunction()
    Dim yr As Integer
    yr = Application.WorksheetFunction.Year(Range("A1"))
    MsgBox yr
End Sub

This works, but it’s slower than the native VBA function. For most tasks, stick with Year.

Frequently Asked Questions

How do I get the year from a date in VBA? Use the Year function: Year(dateValue). It returns the year as an integer. For the current year, use Year(Date).

What if my date is stored as a string? VBA will try to convert a string to a date when you pass it to Year, but it depends on your system’s locale. To be safe, use CDate to convert explicitly: Year(CDate("2024-03-15")).

Why does Year return 1899 for some cells? If a cell contains a time value (like 12:30 PM) or is empty, Excel treats it as a date serial number where the year is 1899. Check the cell’s value with IsDate before extracting the year.

Can I get the year from a date in a different calendar? VBA’s Year uses the Gregorian calendar. If you need a fiscal year or a different calendar system, you’ll need custom logic, such as adding a month offset for fiscal years.

Is there a way to get the year without using VBA? Yes, in Excel you can use the YEAR worksheet function. But if you’re already writing VBA, the Year function is the most direct method.

Alternatives and Related Functions

Besides Year, VBA provides Month and Day to extract other components. You can combine them to build custom date strings. For more complex date calculations, consider using DateAdd, DateDiff, or DateSerial. For example, to get the first day of the current year:

Sub FirstDayOfYear()
    Dim firstDay As Date
    firstDay = DateSerial(Year(Date), 1, 1)
    MsgBox firstDay
End Sub

This uses Year(Date) to get the current year, then DateSerial to construct a date. This is a common pattern for fiscal year calculations.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.