如何在vba中将mm / dd / yy更改为yy / mm / dd,我可以更改格式但不会更改单元格的值

时间:2015-07-15 13:33:09

标签: vba excel-vba excel

我的格式为mm / dd / yy格式的单元格,如何在vba中将单元格更改为yy / mm / dd,我尝试使用ThisWorkbook.Worksheets(1).Cells(11, 4).NumberFormat = "yy/mm/dd",但它不会更改细胞。

1 个答案:

答案 0 :(得分:1)

如果更改单元格的格式并且显示的值没有响应,则值为文本而不是真正的日期。解决这个问题:

Sub dural()
    Dim s As String, d As Date
    With ThisWorkbook.Worksheets(1).Cells(11, 4)
        s = .Text
        d = DateValue(s)
        .Clear
        .Value = d
        .NumberFormat = "yy/mm/dd"
    End With
End Sub

修改#1:

使用纯字符串执行此操作:

Sub dural2()
    Dim s As String
    With ThisWorkbook.Worksheets(1).Cells(11, 4)
        s = Trim(.Text)
        ary = Split(s, "/")
        .NumberFormat = "@"
        .Value = ary(2) & "/" & ary(0) & "/" & ary(1)
    End With
End Sub