尝试将某些格式转换为date格式
我已经能够得到下面的代码,成功地将一般值“19082015”和“9052015”转换为date格式dd / mm / yyyy。
但是,当我尝试在相同的代码中转换一般值“19.08.2015”时,它不运行macros,并显示:
运行时错误“13”:types不匹配
在行“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
如果Range("A1")
不是一个数字,问题将会出现。 所以,如果它有.
那么你就不能把这个价值放在很长的时间里。 声明l
为string
不long
,你的代码将工作
[a2] = CDate(Join(Split([a1], "."), "-"))