我已经能够获得下面的代码,成功地将一般值“19082015”和“9052015”转换为日期格式dd/mm/yyyy。试图将某些格式转换为日期格式
但是当我尝试在相同的代码转换的一般价值“2015年8月19日”,它不运行宏,并显示:
运行时错误“13”: 类型不匹配
在行“l =范围(”A1“)。值”
需要做些什么才能处理“19.08.2015”格式呢?
Sub convertdate()
Dim l As Long
Dim testdate As String
Dim convertdate As Date
l = Range("A1").Value
testdate = CStr(l)
dotdate = False
If InStr(testdate, ".") Then dotdate = True
If dotdate = False Then convertdate = DateValue(CInt(Left(testdate, Len(testdate) - 6)) & "/" & CInt(Mid(testdate, Len(testdate) - 5, 2)) & "/" & CInt(Right(testdate, 4)))
If dotdate = True Then convertdate = DateValue(CInt(Left(testdate, Len(testdate) - 8)) & "/" & CInt(Mid(testdate, Len(testdate) - 6, 2)) & "/" & CInt(Right(testdate, 4)))
Range("A2").Value = convertdate
End Sub
不太知道我怎么错过了!谢谢,现在完美。 – redd