I’ve had a look around and can’t find an actual answer to this.
I have a .csv to download every day. In it includes a text field 7+2 digits long. The 7 digits are in format CYYMMDD and the 2 digits are blank spaces.
This would be today’s date:
"1190729 " (without the quotes)
I’ve tried about 20 ways to convert this to a regular date, but I can’t get every record to update properly
The QueryTable uses type 2 (Text)
=TRIMthe text, place it back in the same location
I change the text into a recognisable date
“Paste as Text”
Then I get the same thing every time. Any date where the DAY is 13 or more, is a text field. 12 and under is a date field
The best way to fix it is to
F2but this is meant to be a totally automated report
I have tried changing
dd/mm/yyyy to every other version I can think of (as for my excel
m/d/yyyy is the default)
I’ve also tried moving
.Value = .Value to the bottom, putting it in twice etc. but to no avail
Private Sub CopyDateTime(ByVal lastRowBeforeImport As Long) Dim ws As Worksheet, ws2 As Worksheet Dim tempsheet As String Dim lastRowAfterImport, lastRowTemp Set ws = Worksheets("Report") 'Bottom populated cell of Column "B" lastRowAfterImport = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row tempsheet = "temp" Worksheets.Add ActiveSheet.Name = tempsheet Set ws2 = Worksheets("temp") With ws.Range(ws.Cells(lastRowBeforeImport, 5), ws.Cells(lastRowAfterImport, 6)) 'Copy new date and time to tempsheet' .Copy Destination:=ws2.Range("A1") End With lastRowTemp = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row With ws2.Range(Cells(1, 3), Cells(lastRowTemp, 3)) .FormulaR1C1 = "=TRIM(RC[-2])" .Value = .Value .Copy Destination:=ws2.Range("A1") .Delete End With With ws2.Range(Cells(1, 3), Cells(lastRowTemp, 3)) .FormulaR1C1 = "=RIGHT(RC[-2],2)&""/""&MID(RC[-2],4,2)&""/20""&MID(RC[-2],2,2)" .Value = .Value .NumberFormat = "dd/mm/yyyy" End With With ws2.Cells(1, 4) .FormulaR1C1 = "=MID(TEXT(RC[-2],""000000""),3,2)&"":""&LEFT(TEXT(RC[-2],""000000""),2)&"":""&RIGHT(RC[-2],2)" End With 'Delete tempsheet' Application.DisplayAlerts = False 'Worksheets(tempsheet).Delete Application.DisplayAlerts = True End Sub
If you have “1190729 ” in A2 then the formula
will produce the date 29th July 2019 in your current date format
Date variables are stored as IEEE 64-bit (8-byte) floating-point
numbers that represent dates ranging from 1 January 100, to 31
December 9999, and times from 0:00:00 to 23:59:59.
Your VBA function may parse well string formats from CSV, but you are creating a string value which is not a proper Excel data type for dates.
=DATEVALUE function to convert the string representation of the DATE (ex. 29/7/2019) to the actual Excel date.
For example in your case:
cell.Value = DATEVALUE(TRIM(RC[-2]))
Once in the proper data type, you can change the date format using the
More about DATEVALUE from MSDN.