Forum Discussion
VBA to convert MMM DD YYYY to date
- Apr 07, 2025
Thank you. Try this:
Sub Convert2Date() Dim rng As Range Dim cel As Range Application.ScreenUpdating = False On Error Resume Next Set rng = Range(Range("D2"), Range("D" & Rows.Count).End(xlUp)) rng.NumberFormat = "m/d/yyyy" For Each cel In rng cel.Value = CDate(cel.Value) Next cel Application.ScreenUpdating = True End Sub
Sub Test()
Dim rng As Range
Dim parts() As String
Application.ScreenUpdating = False
For Each rng In Range(Range("D2"), Range("D" & Rows.Count).End(xlUp))
If Application.IsText(rng.Value) Then
parts = Split(rng.Value)
If UBound(parts) > 0 Then
rng.Value = DateValue(parts(1) & "-" & parts(0) & "-" & parts(2))
Else
parts = Split(rng.Value, "/")
If UBound(parts) > 0 Then
rng.Value = DateSerial(parts(2), parts(1), parts(0))
End If
End If
End If
Next rng
Application.ScreenUpdating = True
End Sub
- MHT1974Mar 25, 2025Copper Contributor
Good morning Hans, thx so much for the speedy response.
When testing this I get a Run Time Error '13', see belowAnd here's a snippet of what the spreadsheet Col D looked like before running the VBA.
Would appreciate any further advise pls
- HansVogelaarMar 26, 2025MVP
Could you attach a small sample workbook demonstrating the problem (without sensitive data), or if that is not possible, make it available through OneDrive, Google Drive, Dropbox or similar?
- MHT1974Apr 02, 2025Copper Contributor
Hi Hans, thx so much for your response and sorry for the delay, was on leave.
I have an existing Macro that copies invoice data from various files together into one file.
As you will see from the attached Demo file, sometimes the date comes across correctly and sometimes it doesn't.
The count of headers tell me how many files it came from and I usually ignore these.