如何将20120103这样的数字转换为Excel可识别的date?

我有一个8位数字,告诉我date,YYYYMMDD。 我怎样才能将这个数字转换成Excel将会识别为date的date。

我们假设单元格A1中有20120229 …我该怎么办?

既然你标记了这个问题的VBA,我想你想在VBA的答案,所以在这里你去:

Dim l As Long Dim s As String Dim d As Date l = Range("A1").Value ' 20120229 ' convert it to a string s = CStr(l) ' can now use string functions to parse it d = DateSerial(CInt(Left(s, 4)), CInt(Mid(s, 5, 2)), CInt(Right(s, 2))) ' d is now 29 Feb 2012 ' write it back to the sheet Range("A2").Value = d 

使用此公式: =DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))其中A1是单元格坐标。

我喜欢能够在excel中select文本并调用macros来完成这项工作。

 Sub YYYYMMDDToDate() Dim c As Range For Each c In Selection.Cells c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2)) 'Following line added only to enforce the format. c.NumberFormat = "mm/dd/yyyy" Next End Sub