I've got a date column that contains dates in mixed format. For example:
A
21.03.1990
03/21/1990
So, basically there are two different formats in one column: dd.mm.yyyy
and mm/dd/yyyy
. I'm trying to write a VBA script to change format of all dates in the column to be yyyy-mm-dd
. That's what I've got so far:
Sub changeFormat()
Dim rLastCell As Range
Dim cell As Range, i As Long
Dim LValue As String
i = 1
With ActiveWorkbook.Worksheets("Sheet1")
Set rLastCell = .Range("A65536").End(xlUp)
On Error Resume Next
For Each cell In .Range("A1:A" & rLastCell.Row)
LValue = Format(cell.Value, "yyyy-mm-dd")
.Range("B" & i).Value = LValue
i = i + 1
Next cell
On Error GoTo 0
End With
End Sub
I know that it's not elegant piece of code, but I'm beginner with VBA so please forgive me.
The problem with that code is that it just rewrite unchanged A column into column B, when I change argument in Format function from yyyy-mm-dd
to dd/mm/yyyy
it works, but only for dates in format mm/dd/yyyy
, and leaves dd.mm.yyyy
untouched. I would appreciate any advice.