持续时间计算使用VBScript的错误运行时

时间:2013-01-02 07:17:38

标签: excel-vba vbscript vba excel

我正在尝试通过以下Sub计算持续时间。但它只需要两次迭代。因为它应该采用4,因此所有持续时间都没有被总结。我介绍了2 msgbox - 它们显示了结果。最合适的一个显示完美的长度,但第二个显示一些不受欢迎的东西。

CODE

msgbox(ArrayListTaskDetails.Count) '~~ output is 16
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 Step 4

dt1 = ArrayListTaskDetails(IndexSearch + 3)         
SumDate = TimeAdd(dt1,SumDate)

If Err Then

msgbox(IndexSearch) '~~ output as 8. here is the question why the error occurs?
Err.Clear
Exit for

End If

Next

Function TimeAdd(dt1,dt2)

If (IsDate(Cdate(dt1)) And IsDate(Cdate(dt2))) = False Then ' `Type mismatch: Cdate` error occurs in this line. Could you guide me here where the wrong is?
TimeAdd = "00:00:00"
Exit Function
End If

TimeAdd = Hour(dt1)+Hour(dt2) & ":" & Minute(dt1)+Minute(dt2) & ":" & Second(dt1)+Second(dt2) 

End Function

注意: 持续时间分别为00:05:2300:11:58195:33:12320:37:13,分别为3,7 ,List

的第11,15位

1 个答案:

答案 0 :(得分:1)

IsDate函数返回一个布尔值,指示计算的表达式是否可以转换为日期。如果表达式是日期或可以转换为日期,则返回True;否则,它返回False.Link h ere

代码应为: -

If Not (IsDate((dt1) And IsDate(dt2)) Then            '' If dt1 or dt2, any one from this id not date then it will set timeAdd = "00:00:00"
   TimeAdd = "00:00:00"
   Exit Function
End If