如果我有一个包含以下内容的单元格:
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
假设文本在 A1 中,我们可以将其拆分为单独的部分,然后使用DATEVALUE
将其放在一起。
B1: =MID(A1,FIND(" ",A1)+1,FIND(" ",A1,FIND(" ",A1)+2)-FIND(" ",A1))
B2: =IFERROR(VALUE(MID(A1,FIND(" ",A1,FIND(" ",A1)+2),3)),VALUE(MID(A1,FIND(" ",A1,FIND(" ",A1)+2),2)))
B3: =RIGHT(A1,4)
B4: =DATEVALUE(B2&" "&B1&" "&B3)
或者,您可以一次性完成:
=DATEVALUE(IFERROR(VALUE(MID(A1,FIND(" ",A1,FIND(" ",A1)+2),3)),VALUE(MID(A1,FIND(" ",A1,FIND(" ",A1)+2),2)))&" "&MID(A1,FIND(" ",A1)+1,FIND(" ",A1,FIND(" ",A1)+2)-FIND(" ",A1))&" "&RIGHT(A1,4))