我有一个Excel工作表,其中一列的日期格式为"yyyyMMdd",我想将其格式化为"yyyy/MM/dd"

为此,我尝试在宏内使用以下行,但是它将单元格数据转换为“ ### .....#”,而不是更改日期格式。

Sheet1.Range("C3", "C302").NumberFormat = "yyyy/mm/dd"
...
result = "#####...#"
...


有人可以告诉我为什么会这样吗?还有其他方法吗?

最佳答案

有了所有好的答案,我将添加简单的vba解决方案...

Option Explicit
Sub FormatDate()
    Dim xlRng As Range
    Dim xlShtRng As Range

    '//- Date format 20160112
    Set xlShtRng = [A3:A10] '//- or [A3, A6, A10]

    For Each xlRng In xlShtRng
        xlRng.Value = DateSerial(Left(xlRng.Value, 4), Mid(xlRng.Value, 5, 2), Right(xlRng.Value, 2))
        xlRng.NumberFormat = "yyyy/mm/dd" '//- 2016/01/12
    Next
End Sub

09-28 05:04