The Year function in Excel VBA returns the four-digit year from any date value, so Year("15-Mar-2024") gives you 2024 as a whole number. You call it with a date, a cell that holds a date, or the result of another date function like Date() or Now(), and you write the result into a cell, a variable, or a message box. That is the entire job: one date in, one year out. The rest of this guide covers how to wire it up in the editor, how to handle empty cells and text dates that trip people up, and what to do when the answer looks wrong.

What You'll Need

  • Excel on Windows or Mac with the Visual Basic Editor available (the Developer tab, or Alt+F11 on Windows / Option+F11 on Mac). Labels and tab positions can differ slightly by version, device and region.
  • A workbook saved as .xlsm (macro-enabled) so your code survives closing and reopening. A plain .xlsx will prompt you to save as macro-free and drop the code.
  • Some data to test on. A column of real dates is ideal; dates stored as text will need extra handling.
  • Macro security set to allow your own macros. If macros are blocked, the code will not run at all and you will get no error, just nothing.

Step-by-Step

  1. Open the editor. Press Alt+F11 (Windows) or Option+F11 (Mac) to open the Visual Basic Editor. On Windows you can also enable the Developer tab via File > Options > Customize Ribbon and tick Developer, then click Visual Basic there. Menu wording varies by version.

  2. Insert a module. In the editor choose Insert > Module. This creates a blank code window inside your workbook. Code written in a worksheet module also works, but a standard module is easier to reuse and to call from buttons.

  3. Write the simplest version first. Type a small test so you can see the result immediately:

    Sub ShowYear()
        MsgBox Year(Date)
    End Sub
    Press F5 to run it. A message box should show the current year. If nothing happens, check that macros are enabled for the file.

  4. Read the year from a cell. Point the function at a real date. For example, if A2 holds 15-Mar-2024:

    Sub YearFromCell()
        MsgBox Year(Range("A2").Value)
    End Sub
    This returns 2024. Change the cell reference to suit your sheet.

  5. Write the year back into a sheet. Most real use is filling a column. This loops down A2 to the last used row and puts the year in column B:

    Sub FillYears()
        Dim lastRow As Long, i As Long
        lastRow = Cells(Rows.Count, "A").End(xlUp).Row
        For i = 2 To lastRow
            If IsDate(Cells(i, "A").Value) Then
                Cells(i, "B").Value = Year(Cells(i, "A").Value)
            End If
        Next i
    End Sub
    The IsDate check skips blanks and text that is not a date, which prevents the runtime error described below.

  6. Store the result in a variable. If you need the year for comparison rather than display, assign it to a Long:

    Dim y As Long
    y = Year(Range("A2").Value)
    If y >= 2024 Then MsgBox "Recent record"
    Do not declare it as Integer; Year returns a value that fits comfortably in Long and Long avoids overflow surprises.

  7. Combine with other date functions. Year sits alongside Month and Day and pairs well with DateSerial. For example, to get the first day of the year belonging to a date in A2:

    Range("C2").Value = DateSerial(Year(Range("A2").Value), 1, 1)
    To get the number of years since that date, use DateDiff("yyyy", Range("A2").Value, Date). Year gives the year number; DateDiff gives the span.

  8. Test with edge cases before trusting it. Try a blank cell, a cell containing the text "March 2024", a cell with today's date, and a cell containing a formula that returns a date. Note which ones fail so you can add guards where needed.

Common Mistakes

  • Passing an empty cell. Year reads an empty variant as zero, and Year(0) returns 1899 (the date Excel counts from). A blank cell therefore looks like a valid year instead of an error. Guard with IsDate or check IsEmpty first.
  • Passing text that only looks like a date. If a column was imported and the values are text, Year throws a type mismatch. Check with IsDate or TypeName and convert with CDate before calling Year.
  • Expecting a padded string. Year returns a number, not text. If you need "2024" as text for concatenation, wrap it in CStr. If you need a two-digit year, Year is the wrong function; use Format(date, "yy").
  • Using Year on a date that is really a serial number displayed oddly. Year converts the serial correctly, so the function is fine, but a cell showing 45292 is a formatting problem, not a code problem.
  • Declaring the variable as Integer. Keep year variables as Long to match the function's return type and avoid overflow errors in calculations.
  • Writing code in the wrong module. Code in ThisWorkbook or a sheet module may not appear in the macro list from the Macros dialog. Standard modules are the safe home for general routines.

Tips

  • Use Year(Now) when you want the year at the moment the macro runs, and Year(Date) when you only need today's date without the time. They return the same year.
  • Pair Year with WorksheetFunction.Text if you want the year rendered with a label or formatting, rather than switching between VBA and worksheet functions mid-loop.
  • Turn off ScreenUpdating while looping and turn it back on at the end; on large sheets this makes the FillYears routine feel instant instead of crawling.
  • Write to a variant array and paste once, rather than writing cell by cell, if you are processing thousands of rows.
  • Keep a small test module with a few known dates so you can confirm the function behaves after an Office update.

Troubleshooting

Run-time error 13, type mismatch. The value you passed is not a date: usually an empty cell, a text date, or a number Excel does not treat as a date. Add an IsDate check, or convert the value with CDate before calling Year.

You get 1899 or 1900 for some rows. Those rows are blank or contain zero, and Excel is interpreting zero as a date. Skip blanks before processing, or write an explicit empty string into the result column instead of a year.

The macro does not appear in the list. Press Alt+F11 and confirm the code sits in a standard module and that the Sub is not marked Private. Then check File > Options > Trust Center settings to confirm macros are enabled for this location.

A security warning blocks the file. Files from the internet carry a mark that disables macros. Open the file's properties and unblock it, or move it to a trusted location. Only do this for files you created or trust.

The code runs but nothing changes. Check that the target cells are not locked in a protected sheet, and that you are writing to the sheet you think you are, not another open workbook. Reference the sheet explicitly, as in Worksheets("Data").Cells(i, "B").

Frequently Asked Questions

Does Year work with a date stored as text?

Not directly. Text like "March 2024" or an imported string causes a type mismatch. Wrap it in CDate first if it is in a format Excel recognises, or clean the data on the sheet before running the code.

What is the difference between Year and DatePart?

Both return the year. DatePart is more flexible because you can ask for "yyyy", "q" for quarter, or "ww" for week using the same call, which suits reports that need several date parts at once.

Can Year return a two-digit year?

No. It always returns the full four-digit year as a number. Use Format(date, "yy") when you specifically need the short form.

Why does Year(0) return 1899?

Excel's date system starts counting from the beginning of the twentieth century, so serial zero corresponds to a date in 1899. This is the underlying reason blank cells produce a plausible-looking but wrong year.

Can I use Year inside a worksheet formula?

Yes, but on the sheet you use the worksheet function =YEAR(A2), not the VBA syntax. The VBA function is for macros; the worksheet version is for cell formulas.

Does Year work on Mac?

Yes. The function is part of the VBA language, so it behaves the same on Windows and Mac. Only the editor shortcuts and some menu positions differ.