将文本日期转换为日期

时间:2014-04-24 19:18:37

标签: excel

我目前有多条记录格式如下:

      24th April 2014

但是,我希望它的格式如下:

      24/04/2014 or 24/04/14 (either way is fine, does not really matter)

我顺便使用excel 2013.

感谢您的反馈。

3 个答案:

答案 0 :(得分:0)

如果您能够从日期中删除“th”,Excel将能够自动格式化它。如果你有很多数据,一个简单的vba脚本就可以解决问题。

答案 1 :(得分:0)

选择要转换的单元格并运行宏 ReDate

Sub ReDate()
    Dim r As Range
    For Each r In Selection
        ary = Split(r.Value, " ")
        r.Clear
        r.Value = DateSerial(ary(2), Monthh(CStr(ary(1))), Numbrs(CStr(ary(0))))
    Next r
End Sub

Public Function Monthh(s As String) As Long
    Dim mnth(1 To 12) As String
    mnth(1) = "January"
    mnth(2) = "February"
    mnth(3) = "March"
    mnth(4) = "April"
    mnth(5) = "May"
    mnth(6) = "June"
    mnth(7) = "July"
    mnth(8) = "August"
    mnth(9) = "September"
    mnth(10) = "October"
    mnth(11) = "November"
    mnth(12) = "December"
    Monthh = Application.WorksheetFunction.Match(s, mnth, 0)
End Function

Public Function Numbrs(sIn As String) As Long
    Dim L As Long, v As String, i As Long
    Dim CH As String
    L = Len(sIn)
    v = ""
    For i = 1 To L
        CH = Mid(sIn, i, 1)
        If CH Like "[0-9]" Then
            v = v & CH
        End If
    Next i
    Numbrs = CLng(v)
End Function

答案 2 :(得分:0)

我认为DATEVALUE函数应该对这个问题有好处。

=DATEVALUE(LEFT(A1,FIND(" ",A1)-3)&RIGHT(A1,LEN(A1)-FIND(" ",A1)+1))

我们假设日期文本存储在A列中。我们知道主要问题是存在“st”或“nd”或“th”或“rd”。如果它们不存在,Excel可以自动识别此日期值。所以我尝试使用文本函数创建一个带有“th”的日期文本:

LEFT(A1,FIND(" ",A1)-3)&RIGHT(A1,LEN(A1)-FIND(" ",A1)+1)

然后使用DATEVALUE函数将它们转换为datevalue,我想这就完成了。

是不是?希望这可以帮助你!