Forum Discussion
Convert Text to Date Format
- Jun 07, 2019
Hi Nina,
you can use this macro, see attached file to convert dates. It converts all dates in selection to real date.
Sub KonvertDate()
Dim rngCell As Range
For Each rngCell In Selection
rngCell.Value = CDate(rngCell.Value)
Next rngCell
End SubBest regards from germany
Bernd
http://www.vba-tanker.com
One extra note to flag, don't open then save your CSVs, use Power Query to pull the data in without opening the original CSVs.
In your example most solutions will leave the circled values as 3rd January, 3rd February but I'm guessing the original dates were 1st and 2nd of March
- Nina_SCJul 14, 2019Copper Contributor
Wyn Hopkins Hi wyn, thank you so much for your suggestion. I tried applying it and used power query in power bi -- and after changing the locale still nothing has changed. The error was still there. I don't know where the error is coming from but would you mind taking a look at my file? It would mean a lot and thank you so much.
- Wyn HopkinsJul 15, 2019MVP
If you don't have the original unsaved then you may need to use a formula approach like the attached
- Wyn HopkinsJul 15, 2019MVP
Hi Nina_SC
Unfortunately once you've saved the data in Excel all of those 1st of June dates have been already converted to 6th of January which is wrong. Power Query can't fix this
What you need is the original unsaved CSV file and then use Power Query to pull that data in.
Do you have the original CSV you can post here?
- SergeiBaklanJul 14, 2019Diamond Contributor
With Power Query click Data->From Table/Range (it'll be converted to table), Transform, in Power Query editor select all 3 columns, and from right click menu select Change Type->Using locale. Here Date and English (United States). Land the result back to the sheet.
Generate script is
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], #"Changed Type with Locale" = Table.TransformColumnTypes(Source, { {"Created Time", type datetime}, {"Last Update Time", type datetime}, {"Close Time", type datetime}} , "en-US") in #"Changed Type with Locale"
Please see Sheet2 in attached.