VBA & Macro Problems

Convert Two-Digit Excel Dates to Four-Digit Years with VBA

Maira MehtabMaira Mehtab Nov 14, 2026 870 views

Question details

Convert a column of imported DD/MM/YY values into Excel dates displayed with four-digit years without stopping at blank cells.

Convert Two-Digit Excel Dates to Four-Digit Years with VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A data file contains dates imported as General, and a macro needs to process the date column.
Observed behavior
The macro returns runtime error 13, a type mismatch, when it reaches a blank cell.
Before you start

Identify the date column and its last row, then check whether its values are DD/MM/YY text or existing numeric Excel date serials. Decide how two-digit years should map to centuries; the macro below treats 00–29 as 2000–2029 and 30–99 as 1930–1999.

Solution 1Recommended

Skip blanks and convert DD/MM/YY values with a VBA macro

Use this macro when the column contains blank cells, DD/MM/YY text, or numeric Excel date serials.

The macro parses text as day, month, and year explicitly, so it does not depend on the computer's date order. It leaves unsupported or invalid values unchanged and reports how many were skipped.

The century cutoff is defined in the code. Adjust it if your data uses a different rule.

1
Open the VBA editor

In Excel, open the workbook and press Alt+F11. In the VBA editor, choose Insert > Module.

2
Add the conversion macro

Paste the following code into the module: Sub ConvertDates() Dim c As Range, s As String Dim d As Long, m As Long, y As Long Dim dt As Date, skipped As Long For Each c In ActiveSheet.Range("C2:C10000") If IsError(c.Value2) Then skipped = skipped + 1 ElseIf Len(Trim$(CStr(c.Value2))) > 0 Then If VarType(c.Value2) = vbDouble Then If c.Value2 >= 0 And c.Value2 <= 2958465 Then c.NumberFormat = "dd/mm/yyyy" Else skipped = skipped + 1 End If Else s = Trim$(CStr(c.Value2)) If s Like "##/##/##" Then d = CLng(Left$(s, 2)) m = CLng(Mid$(s, 4, 2)) y = CLng(Right$(s, 2)) If y <= 29 Then y = y + 2000 Else y = y + 1900 End If If m >= 1 And m <= 12 And d >= 1 And d <= 31 Then dt = DateSerial(y, m, d) If Day(dt) = d And Month(dt) = m And Year(dt) = y Then c.Value = dt c.NumberFormat = "dd/mm/yyyy" Else skipped = skipped + 1 End If Else skipped = skipped + 1 End If Else skipped = skipped + 1 End If End If End If Next c MsgBox "Unsupported or invalid cells skipped: " & skipped End Sub

3
Set the target range

In the code, replace C2:C10000 with the actual date range. The macro operates on the active worksheet, so select the correct sheet in Excel before running it.

4
Run and check the results

In Excel, open the Macros dialog, select ConvertDates, and choose Run. Check the reported skipped count and inspect representative dates, especially around the century cutoff.

Skip blanks and convert DD/MM/YY values with a VBA macro
Formatting does not determine the century: The dd/mm/yyyy format displays the year of an existing date; it does not correct an incorrectly interpreted century. The macro converts only text matching exactly DD/MM/YY and leaves other text formats unchanged.
Work with dates in WPS Spreadsheet

Convert imported date text in WPS Spreadsheet

Use Text to Columns to interpret DD/MM/YY text as dates, then apply a four-digit-year date format. Check the resulting centuries because two-digit-year interpretation may depend on system settings.

  1. 1. Open the workbook: Open WPS Spreadsheet and choose Open to load the Excel workbook.
  2. 2. Select the date cells: Select the imported date cells in the column, excluding the header if it contains text.
  3. 3. Convert the text dates: Choose Data > Text to Columns. Proceed through the wizard without splitting the values, select Date with DMY order for the column, and finish the conversion.
  4. 4. Apply the date format: With the cells selected, open Format Cells and choose a custom number format of dd/mm/yyyy. Check sample dates and their centuries after conversion.
Convert a selected column of imported date text without writing a macroDisplay recognized dates with a four-digit yearOpen and save Microsoft Excel XLSX and XLS workbooksWork in a familiar spreadsheet interface
microsoft office alternative - wps office

Frequently Asked Questions

Why does the VBA macro stop at a blank cell?

Converting an empty value with date-conversion code can cause a type mismatch. Check that the cell is not an error and that its trimmed value is not empty before attempting conversion.

Does changing the format to dd/mm/yyyy convert text into a date?

No. The format changes how numeric date values are displayed. Convert DD/MM/YY text first using the macro or a date conversion tool, then apply the format.

How does the macro decide which century to use?

The example maps years 00–29 to 2000–2029 and 30–99 to 1930–1999. Change the cutoff and year offsets in the If y <= 29 block if your data follows another rule.

What happens to invalid dates or error cells?

The macro leaves them unchanged and adds them to the skipped count shown after it runs. Review the source values and correct them before rerunning the conversion.

Will the macro convert dates written with dashes or four-digit years?

No. The example accepts text matching exactly DD/MM/YY. Change the parsing logic or standardize those values before running it; existing numeric date serials are formatted without being reparsed.