2

我有一个 8 位数字告诉我日期,YYYYMMDD。如何将此数字转换为 Excel 将识别为日期的日期。

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

4

3 回答 3

9

既然你用 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
于 2012-06-18T19:11:46.180 回答
5

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

于 2012-06-18T19:01:50.333 回答
1

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

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
于 2017-05-22T19:50:07.477 回答