Book1
A B 1 1/1/1925 1/1/2025 Sheet1
Cell Formulas Range Formula B1 B1 =Add100Years(A1)
VBA Code:Option Explicit Public Function Add100Years(ByRef rng As Range) As Date Dim sDate As Date sDate = rng.Value Add100Years = DateAdd("yyyy", 100, sDate) End Function
From the sample file that I downloaded all values in column A are TEXT values and all only have a 2 year date figure. So from your question I assumed that all existing dates were 19xx dates and you wanted them converted... but some dates are already in 20xx...
From the sample file that I downloaded all values in column A are TEXT values and all only have a 2 year date figure. So from your question I assumed that all existing dates were 19xx dates and you wanted them converted.
Could you indicate which ones are already 20xx and how we can tell that from what is in the cells?
Unfortunately, that does not answer either of my two questions.I want everything to be in 20xx
Could you indicate which ones are already 20xx and how we can tell that from what is in the cells?
On the one hand you say the date is in the format mm/dd/yy but you then provided a file with all the dates being Text (not dates) and in the format dd-mmm-yy.the date in original form is mm/dd/yy
Sub TextDateMake2000()
With Range("A2", Cells(Rows.Count, "A").End(xlUp))
.Value = Application.Replace(.Value, 8, 0, "20")
End With
End Sub
Sub DateMake2000()
Dim rng As Range
Dim arr As Variant
Dim dtTemp As Date
Dim i As Long
Set rng = Range("A2", Cells(Rows.Count, "A").End(xlUp))
arr = rng.Value
For i = 1 To UBound(arr)
dtTemp = CDate(arr(i, 1))
If Year(dtTemp) < 2000 Then dtTemp = DateSerial(Year(dtTemp) + 100, Month(dtTemp), Day(dtTemp))
arr(i, 1) = dtTemp
Next i
rng.Value = arr
End Sub
nothing should be in 19xxUnfortunately, that does not answer either of my two questions.
On the one hand you say the date is in the format mm/dd/yy but you then provided a file with all the dates being Text (not dates) and in the format dd-mmm-yy.
What is your process for getting the data ? What is the earliest step in which the macro could be run and can you give us a file with the dates at that point ?
Based on your sample data in Post #8 either of these should work.
The 2nd will work even if some some of the text values have been converted to dates.
Option 1: Text values per sample file dd-mmm-yy
VBA Code:Sub TextDateMake2000() With Range("A2", Cells(Rows.Count, "A").End(xlUp)) .Value = Application.Replace(.Value, 8, 0, "20") End With End Sub
Option 2: Values per sample file either text or date
VBA Code:Sub DateMake2000() Dim rng As Range Dim arr As Variant Dim dtTemp As Date Dim i As Long Set rng = Range("A2", Cells(Rows.Count, "A").End(xlUp)) arr = rng.Value For i = 1 To UBound(arr) dtTemp = CDate(arr(i, 1)) If Year(dtTemp) < 2000 Then dtTemp = DateSerial(Year(dtTemp) + 100, Month(dtTemp), Day(dtTemp)) arr(i, 1) = dtTemp Next i rng.Value = arr End Sub