The Excel VBA Year function returns the year portion of a date as a four-digit integer. Use it when you need just the year from a date value, such as 2024 from "15-Mar-2024", without doing text parsing or worksheet formulas. You call it like any other VBA function: Year(dateValue), where dateValue is any expression that VBA can interpret as a date. The result is a whole number between 100 and 9999, so you can compare years, group records, or build filenames. No add-ins or special references are required; the function is built into VBA and works in Excel for Windows and Mac. The main prerequisite is that the input must be a valid date or something VBA can convert to a date; otherwise you get a runtime error 13 (Type mismatch).

What You'll Need

  • Excel with macros enabled (the Developer tab visible, or access to the VBA editor via Alt+F11).
  • A workbook saved as .xlsm or .xlsb if you want to keep your VBA code.
  • Basic familiarity with the VBA editor: inserting a module and running a macro.
  • Dates stored as real Excel dates (serial numbers), not as text that looks like a date. If your dates are text, convert them first or use DateValue inside the function.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab). The editor opens in a separate window.
  2. Insert a module. In the Project Explorer, right-click your workbook name, choose Insert, then Module. A blank code window appears. If the Project Explorer is hidden, press Ctrl+R.
  3. Write a simple test procedure. Type the following code exactly:
    Sub ShowYear()
        Dim d As Date
        d = DateSerial(2024, 3, 15)
        MsgBox Year(d)
    End Sub
    This declares a date variable, assigns 15 March 2024, and displays the year in a message box.
  4. Run the macro. Place the cursor inside the Sub and press F5, or use Run > Run Sub/UserForm. A message box should show 2024. If you see an error, check that the code is in a standard module, not a sheet module.
  5. Use Year with a cell value. To read a date from cell A1 and put the year in B1, write:
    Sub YearFromCell()
        Range("B1").Value = Year(Range("A1").Value)
    End Sub
    Make sure A1 contains a real date. If A1 is empty, Year(0) returns 1900 because VBA treats 0 as 30-Dec-1899; guard against blanks with If IsDate(Range("A1").Value) Then ....
  6. Combine Year with other functions. To get the current year, use Year(Date) or Year(Now). To build a string like "Report-2024", use "Report-" & Year(Date). To compare years, store the result in an Integer or Long variable: Dim y As Long: y = Year(Range("A1").Value).
  7. Handle text dates safely. If a cell contains the text "15/03/2024", Year(Range("A1").Value) may work because VBA coerces text to date, but it can fail depending on regional settings. The reliable approach is Year(DateValue(Range("A1").Value)) or, better, convert the text to a real date first. Use IsDate to test before calling Year.
  8. Loop through a range. A common pattern is to process many rows:
    Sub ListYears()
        Dim c As Range
        For Each c In Range("A2:A100")
            If IsDate(c.Value) Then
                c.Offset(0, 1).Value = Year(c.Value)
            End If
        Next c
    End Sub
    This writes the year next to each valid date and skips blanks or text that cannot be read as a date.
  9. Save and test. Save the workbook as .xlsm. Run the macro again after reopening to confirm it persists. If you plan to share the file, consider adding a button or assigning the macro to a shortcut.

Common Mistakes

  • Passing a blank cell. Year(Empty) or Year(0) returns 1900, not an error. Always test with IsDate or check for vbNullString.
  • Assuming Year parses any text. Year("March 2024") works, but Year("2024-03-15") may fail or be misread depending on locale. Convert with DateValue and test first.
  • Using a String variable for the result. Year returns an Integer; assigning to a String variable forces implicit conversion and can cause type mismatch if the value is unexpected. Use Long or Integer.
  • Forgetting that Year does not format. It returns a number, not a four-character string with leading zeros. If you need "0099", format it separately with Format(Year(d), "0000").
  • Placing code in the wrong module. Macros in sheet modules can behave differently; standard modules are best for general-purpose functions.
  • Ignoring regional date settings. The same text date can be interpreted as day/month or month/day depending on Windows region. Test on the target machine or store dates as real dates.

Tips

  • Use Year(Date) for the current year and Year(Now) when you also need the time component elsewhere; both return the same year.
  • Combine with Month and Day to build a full date key: Year(d) * 10000 + Month(d) * 100 + Day(d).
  • For sorting or grouping by year, store the result as a Long to avoid overflow in calculations.
  • If you work with dates far in the past, remember VBA dates start at 100-01-01; Year returns 100 for that minimum.
  • Use Application.WorksheetFunction.Year only if you specifically need worksheet error handling; the native VBA Year is faster and simpler.
  • Document your assumptions about date formats in comments so future editors know why DateValue is used.

Troubleshooting

If you get Run-time error 13: Type mismatch, the value passed to Year is not a date and cannot be coerced. Check the cell or variable: use IsDate to test, and convert text with DateValue only when you are sure of the format. If the error occurs on a blank cell, add a guard like If Len(c.Value) > 0 And IsDate(c.Value) Then.

If the result is 1900 unexpectedly, you passed 0 or an empty value. VBA treats 0 as 30-Dec-1899, so Year returns 1900. Fix by skipping blanks.

If the macro does nothing, check that macros are enabled (File > Options > Trust Center > Macro Settings) and that you are running the correct procedure. Press Ctrl+G in the VBA editor to open the Immediate window and type ? Year(Date) to test the function directly.

If the year is off by one day or month, your date text was parsed with the wrong regional order. For example, "03/15/2024" may be read as 3 March in some regions. Convert the text to a real date using DateSerial with explicit parts, or change the cell format to a real date before running the macro.

If you see Compile error: Expected: expression, check parentheses and that you did not name a variable Year. Rename any variable that shadows the function.

Frequently Asked Questions

Does Year work on a date stored as text?

Sometimes. VBA tries to convert text to a date, but success depends on regional settings and the text format. Use IsDate to test and DateValue to convert explicitly for reliable results.

What is the difference between Year and Format(date, "yyyy")?

Year returns an integer (2024). Format returns a string ("2024") and lets you control padding with custom patterns like "0000". Use Year for calculations and Format for display.

Can Year return a value less than 1000?

Yes. VBA supports dates from 100-01-01, so Year can return 100 through 9999. For normal business dates you will see four-digit years.

Why does Year(0) return 1900?

VBA's date serial starts at 0 for 30-Dec-1899. Passing 0 or an empty variant is coerced to that date, so the year is 1900. Always check for blanks before calling Year.

Is Year available in Excel for Mac?

Yes, the VBA Year function is part of the VBA language and works in Excel for Mac as well as Windows. The VBA editor interface differs slightly, but the function behaves the same.

How do I get the year from a date in another workbook?

Reference the other workbook's range or value, for example Year(Workbooks("Data.xlsx").Sheets(1).Range("A1").Value). Ensure the workbook is open or use GetObject carefully; closed workbooks require a different approach.