如果我的单元格包含以下内容:

Tuesday, April 16th 2009


如何将该字符串转换为Excel可以识别的日期格式。我想我必须使用MID()和FIND()函数。

最佳答案

这是可以在VBA上使用的功能,并且可以作为用户定义的功能

Function GetDate(InString As Range) As Date
    Dim newDate As Date
    Dim tmpDate As String

    'Sample Date
    'Tuesday, April 16th 2009

    'Remove the Day of the Week.
    tmpDate = Trim(Mid(InString, InStr(InString, ",") + 1))

    'Get rid of "th"
    tmpDate = Replace(tmpDate, "th ", " ")

    'Get rid of "rd"
    tmpDate = Replace(tmpDate, "rd ", " ")

    'Get rid of "nd"
    tmpDate = Replace(tmpDate, "nd ", " ")

    'Get rid of "st"
    tmpDate = Replace(tmpDate, "st ", " ")

    'Convert string to date
    newDate = DateValue(tmpDate)

    GetDate = newDate
End Function

关于excel - Excel VBA从字符串获取日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12963033/

10-10 18:13