2015-05-01 32 views
2

我已经能够获得下面的代码,成功地将一般值“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 

回答

2

如果Range("A1")不是数字,问题将会出现。所以如果它有.,那么你不能把这个值放到很长的时间。声明lstring不是long,你的代码将工作

+0

不太知道我怎么错过了!谢谢,现在完美。 – redd

0
[a2] = CDate(Join(Split([a1], "."), "-")) 
相关问题