尝试将某些格式转换为日期格式
Trying to convert certain formats into a date format
我已经能够获取下面的代码,成功地将一般值“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")
不是数字,就会出现问题。因此,如果其中确实包含 .
,则您不能将该值放入 long 中。将 l
声明为 string
而不是 long
并且您的代码将起作用
[a2] = CDate(Join(Split([a1], "."), "-"))
我已经能够获取下面的代码,成功地将一般值“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")
不是数字,就会出现问题。因此,如果其中确实包含 .
,则您不能将该值放入 long 中。将 l
声明为 string
而不是 long
并且您的代码将起作用
[a2] = CDate(Join(Split([a1], "."), "-"))