我正在编写小型VBA来管理和转换来自外部表的某些日期,而且我遇到了一些奇怪的日期格式,例如:yyyy / mm / dd 我真正需要的是从a1转换日期并将其粘贴到b2
作为模板我将用于A1和B1列: A1 =要转换的日期B1 =转换数据的粘贴Destinaton
2012.Dct.04
我现在测试的是:
看起来我只是坚持一些非常简单的事情,但现在真的很难继续前进,所以这就是我寻求帮助的原因。
感谢任何想法。
答案 0 :(得分:2)
我会在B1
中使用以下公式:
=DATE(LEFT(A1,4),MATCH(MID(A1,6,3),{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dct"},0),RIGHT(A1,2))
奇怪的是缩写" Dct"十二月因此,您可能需要根据您的语言更改数组文字{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dct"}
。
可以使用VBA
后面的Function
:
Function convert_YYYY_MMM_DD_Text2Date(sText As String) As Variant
Dim iYear As Integer
Dim iMonth As Integer
Dim sMonth As String
Dim iDay As Integer
Dim aMonths As Variant
Dim bFound As Boolean
aMonths = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dct")
On Error GoTo errHandler
iYear = CInt(Left(sText, 4))
sMonth = Mid(sText, 6, 3)
iDay = CInt(Right(sText, 2))
For iMonth = 0 To UBound(aMonths)
If aMonths(iMonth) = sMonth Then
bFound = True
Exit For
End If
Next
If bFound Then
convert_YYYY_MMM_DD_Text2Date = DateSerial(iYear, iMonth + 1, iDay)
Else
GoTo errHandler
End If
On Error GoTo 0
Exit Function
errHandler:
convert_YYYY_MMM_DD_Text2Date = "conversion not possible"
End Function