I have an excel file with some dates in the format "06 March, 2016" which I want to convert to "d/MM/yyyy" or "6/3/2016" in order to use excel formulas like DATEVALUE()
on it to extract parts of the date.
I wrote a small macro to help me with this which just replaces the dates as I would manually in the input dataset.
Sub MonthReplace()
Dim res As Boolean
Dim i As Long
Dim monthArray As Variant
monthArray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
For i = LBound(monthArray) To UBound(monthArray)
res = Range("RawDataset").Replace(" " + monthArray(i) + ", ", "/" + Format(i + 1) + "/")
Next i
End Sub
The result is an unstable dataset. Please see the images, before and after.
Some are getting converted correctly, while other are getting their month and day interchanged. This behavior does not occur when I used to replace the months without using the Macro. The default date format in Excel is set as per my desired format.
System regional settings for Date:
m/d/yyyy
and notd/m/yyyy
. See 06 March. 2016 => 3/6/2016 and 02 March. 2016 => 3/2/2016. So 17 March. 2016 cannot be converted to date format from 17/3/2016 because month 17 is not known. – Axel Richteren_us
withm/d/yy
. So you must either set this format or useCDate("dd/mm/yyyy")
to convert. – Axel Richter