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

时间:2015-11-12 11:03:32

标签: excel vba excel-vba date

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?

4 个答案:

答案 0 :(得分:1)

您没有发布关于如何打开CSV文件的代码 - 这是关键区域。在输入工作表之前,需要正确解析日期。以下代码将选择然后打开一个文件,该文件在单个列中具有英国样式日期,并正确解析它们。您需要根据您的特定要求进行调整。

FieldInfo参数是什么工作。 Excel工作表的格式为“for show”,因此您可以看到明确的日期。

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

答案 1 :(得分:1)

尝试使用Workbooks.OpenText()方法,并将Local标记设置为True

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

Here is the MSDN article on this method代表Local设置:

  

如果机器的区域设置应用于分隔符,数字和数据格式,请指定 True

答案 2 :(得分:0)

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

答案 3 :(得分:0)

也许您可以转换CSV文件以将日期显示为数字,即。 11月10日15将显示为42318.或者添加一个单独的列,其中B1为=DATEVALUE(A1)并使用该列。

创建摘要报告时,请导入数字并使用CDateFormat将其转换为日期。像这样:

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

编辑:

为了更好的格式化(不需要NumberFormat,我认为它会立即使用您的区域设置)并自动将单元格格式设置为日期类型,请使用:

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

结果:
Excel VBA date display

参考文献:
http://www.techonthenet.com/excel/formulas/format_date.php http://www.techonthenet.com/excel/formulas/cdate.php