简体   繁体   中英

Confusing dd/mm/yyyy and mm/dd/yyyy

I have a VBA script that opens up a bunch of CSV files, and compiles them into one summary report.

However, I'm having a problem where it reads in UK style dates (dd/mm/yyyy), then interprets them as US-style dates when it makes the copy, before display them as UK-style dates again!

So 4th of July in original sheet becomes 7th of April in the summary sheet - verified by changing cell format to display month name.

This is odd, as when you open up the CSV file in Excel, it correctly interprets the UK style date.

Copy is made using code like this

SummarySheet.Cells(Y,X).value = CSVSheet.Cells(W,Z).value

What is going on here?

You did not post the code as to how you are opening your CSV files -- that is the critical area. The dates need to be parsed properly BEFORE being entered on the worksheet. The following code will selects and then opens a file that has UK style dates in a single column, and properly parse them. You will need to adapt it to your particular requirements.

The FieldInfo argument is what does the work. The formatting of the Excel worksheet is "for show" so you can see an unambiguous date.

Option Explicit
Sub OpenUKcsv()
    Dim sFile As String
    Dim WB As Workbook
    Dim WS As Worksheet

sFile = Application.GetOpenFilename()

Workbooks.OpenText Filename:=sFile, DataType:=xlDelimited, comma:=True, other:=False, _
                    fieldinfo:=Array(1, 4)

Set WB = ActiveWorkbook
Set WS = ActiveSheet

With WS.Columns(1)
    .NumberFormat = "dd-mmm-yyyy"
    .EntireColumn.AutoFit
End With

End Sub

Try using the Workbooks.OpenText() method instead and set the Local flag to True

Set csvWB = Workbooks.OpenText(Filename:=myCSVfile, Local:=True)

Here is the MSDN article on this method which says for the Local setting:

Specify True if regional settings of the machine should be used for separators, numbers and data formatting.

You could use .Text (text displayed in Excel cell) or .Value2 (value without formatting) instead of .Value (value with formatting).

But I strongly suggest that you set the format of the cells that you use to what you expect to have at the end with .NumberFormat = "mm/dd/yyyy"

Or you could use CDate function :

SummarySheet.Cells(Y,X).value = CDate(CSVSheet.Cells(W,Z).value)

Or use an UDF with DateSerial :

Sub test_CMaster()
MsgBox ParseDate("4/7/15") & vbCrLf & CDate("4/7/15")
End Sub

Function ParseDate(ByVal DateInCell As String, Optional Separator As String = "/") As Date
Dim D() As String
D = Split(DateInCell, Separator)
    ParseDate = DateSerial(D(UBound(D)), D(1), D(0))
End Function

Maybe you can convert the CSV files to show dates as numbers, ie. 10th Nov 15 will show as 42318. Or add a separate column where B1 is =DATEVALUE(A1) and work with that.

When you create the summary report, import the numbers and convert them to date using CDate and Format . Something like this:

Sub test()
    Range("A2:A4").NumberFormat = "m/d/yyyy"
    Range("A2").Value = Format(CDate(Range("A1").Value), "dd.mm.yyyy")
    Range("A3").Value = Format(CDate(Range("A1").Value), "mm.dd.yyyy")
    Range("A4").Value = Format(CDate(Range("A1").Value), "Long Date")
End Sub

EDIT:

For better formatting (no need for NumberFormat , I think it will use your regional settings right away) and auto-setting the cell format to date-type, use this:

Sub test()
    Dim sDate As Date
    sDate = CDate(Range("A1").Value)
    Range("A2").Value = DateSerial(Year(sDate), Month(sDate), Day(sDate))
End Sub

Result:
Excel VBA日期显示

References:
http://www.techonthenet.com/excel/formulas/format_date.php http://www.techonthenet.com/excel/formulas/cdate.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM