Back again! Thought i might as well post here, but I can move to Windows Software if needs be. I'm hoping some peeps here are as well versed with VBA as Python!
I have pulled together a macro which does everything I want, except it seems to somehow mess dates up of it's own accord. I've stripped back to the bare bones of a macro below, which doesn't seem to have anything in re values/dates etc, and can't understand what is going wrong. Google doesn't seem to help.
Based on code found here -
https://stackoverflow.com/a/52619462
I basically want it to open the CSV, format the file (stripped those lines out and still have the same issue, so it's this part that's the concern) and save as XLSX.
Code:
Sub CSVtoXLSB2()
Dim wb As Workbook
Dim CSVPath As String
Dim XLSXPath As String
Dim sProcessFile As String
CSVPath = "C:\CSV\"
XLSXPath = "C:\XLSX\"
sProcessFile = Dir(CSVPath & "*.csv")
Do Until sProcessFile = "" ' Loop until no file found.
Set wb = Application.Workbooks.Open(CSVPath & sProcessFile)
wb.SaveAs XLSXPath & Split(wb.Name, ".")(0) & ".xlsx", FileFormat _
:=51
wb.Close
sProcessFile = Dir() ' Get next entry.
Loop
Set wb = Nothing
End Sub
Opening the CSV in excel manually gives
Code:
A B
1 Monthly Transactions
2
3 Period From 01/04/2020
4 Period To 29/04/2020
But for whatever reason, the the above macro gives me a file showing
Code:
A B
1 Monthly Transactions
2
3 Period From 04/01/2020
4 Period To 29/04/2020
It appears the date values have changed, as formatting etc has no bearing.
The same occurs further down in the data, but then fixes itself after a few instances sometimes. eg (different examples).
If it means anything, those which are messed up are displayed as ######## until the column width is adjusted, but the dates below display correctly.
Code:
Transaction Date
04/06/2020
04/06/2020
04/06/2020
04/06/2020
04/07/2020
04/07/2020
04/08/2020
04/10/2020
04/10/2020
04/12/2020
13/04/2020
13/04/2020
13/04/2020
15/04/2020
15/04/2020
15/04/2020
17/04/2020
17/04/2020
20/04/2020
21/04/2020
22/04/2020
22/04/2020
23/04/2020
23/04/2020
26/04/2020
28/04/2020
28/04/2020
29/04/2020
29/04/2020
Code:
Transaction Date
04/03/2020
04/06/2020
04/08/2020
04/08/2020
Anything obvious at play here?
EDIT - Just noticed, are the dates that are being changed all those 12 or fewer days - ie the number of months. I presume these are being read as american dates, but the other left as text, as it's not compliant with the american date format.
Why though? This doesn't happen opening manually! My windows region is set to UK.
No further forward on the cause of this, but the "american dates" are being read as number values and the "UK dates" as text values.
EDIT 2 - Changed the export from the source system to dd-mmm-yyyy, then added a cell format change to the excel macro which has resolved the issue, but still keen to understand why this occurs if anyone knows