I have a problem working with some worksheets within a workbook from a 3rd party website source.
When you face similar problems, Record Macro and compare the generated code to yours. Note that it uses Workbook.SaveAs
instead
For Each ws In wb.Worksheets
ws.Activate
wb.SaveAs Current_Directory & LCase(ws.Name) & ".csv", xlCSV ' check this line with Record Macro
Next
This seems to be an inherent 'problem' with vba and I believe the only solution is to format your date field to a text field prior to exporting, obviously change Selection
to the actual range
Sub convertToStr()
Dim c As Range
For Each c In Selection
c.Value = "'" + CStr(c.Value)
Next c
End Sub
A month or so ago I got shot down when I made an aside about Excel sometimes converting UK dates that could be valid US dates to US dates. So 1/4/11 (1 April 11) would become 4/1/11 (4 January 11) but 13/4/11 (13 April 11) would be unchanged. I could not duplicate the problem so had to admit that I must have made a mistake.
I encountered this problem eight or nine years ago and since then I have always passed dates in the format "1Apr11" to avoid the problem. Your question suggested I had been correct but something other than Excel was at the root of the problem.
I set up a worksheet with numbers, dates and strings. It saved as a CSV file just as I would wish.
I went to Control Panel and changed my location to "United States". I saved the worksheet again and this time the dates in the CSV file were "Americanised" as explained above.
I restored my location to "United Kingdom". My third CSV file was again correct.
You claim that your Control Panel settings are correct. I think you need to check again and look for anything else that could be wrong since I now know how to switch this effect on and off.
I have been able to replicate the issue as described. My regional setting are NZ English. Setup is Excel 2010 32 bit, Win7 64bit
I have used csv
files with Excel many times over the years and usually found Excel native csv handling very fickle. I tend to write my own csv handlers.
Here's an example. You will need to modify it to suit your needs. Eg handle what you want and do not want ""
'd and include or exclude empty cells etc
Sub SaveAsCsv(ws As Worksheet, Name As String)
Dim fso As FileSystemObject
Dim fl As TextStream
Dim dat As Variant
Dim rw As Long, col As Long
Dim ln As String
Dim Quote As String
On Error GoTo EH
Set fso = New FileSystemObject
Set fl = fso.CreateTextFile(Name, True)
dat = ws.UsedRange
For rw = 1 To UBound(dat, 1)
ln = ""
For col = 1 To UBound(dat, 2)
' Modify this section to suit your needs
If dat(rw, col) <> "" Then
If IsNumeric(dat(rw, col)) Or _
IsDate(dat(rw, col)) Then
Quote = ""
Else
Quote = """"
End If
ln = ln & Quote & dat(rw, col) & Quote & ","
End If
Next col
fl.WriteLine Left(ln, Len(ln) - 1)
Next rw
EH:
On Error Resume Next
If Not fl Is Nothing Then fl.Close
Set fl = Nothing
Set fso = Nothing
End Sub
It includes an early bound reference to Microsoft Scripting Runtime
, so set the reference or change to late bound if you want.
To use it in your code, relace ws.SaveAs Name, xlCSV
with SaveAsCsv ws, Name
This is the case if you are using the SaveAs from the Excel menu but not when using VBA. Changing the regional setting has no effect to VBA flipping date to American format. But adding the below statement fixed it for me as suggested above.
ws.Columns("B:B").NumberFormat = "dd/mm/yyyy"