我已经能够获得下面的代码,以成功地将通用值“ 19082015”和“ 9052015”转换为日期格式dd / mm / yyyy。
但是,当我尝试在同一代码中转换通用值“ 19.08.2015”时,它不会运行宏并显示:
运行时错误“ 13”:类型不匹配
在“ l = Range(“ A1”)。Value”行上
需要做什么才能使其也处理“ 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
,您的代码将正常工作
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句