我在cadvba中调用excel表格,从中读取一些固定格式的字符串如“06.07.15”(表示时间06年7月15号)。
现在我想要计算这些时间的时间差,首先我把字符串分离,如:day=15(string),month=07(string),year=06(string),这几个变量我是用cstr转换而得到string型的。
接着我想使用datediff函数来计算时间差,这时出问题了,我定义了date1(date型)=month/day/year,结果得到一个时间“01:30:00”
这时我把date1重新定义:date1=#month/day/year#,结果编译出错哪位大侠给我看看,我现在怎么样能得到这个时间差呢?

解决方案 »

  1.   

    代码如下,大家帮我看一下阿Sub aa()    Dim a1 As String
        a1 = "06.05.20"
        
        Dim day As String
        Dim month As String
        Dim year As String
        day = CStr(Right(a1, 2))
        year = CStr(Left(a1, 2))
        month = CStr(Mid(a1, 4, 2))
        
        Dim date1 As Long
        date1 = DateDiff(d, "month / day / year", now)
        MsgBox date1
    End Sub
      

  2.   

    这代码瞅着,真有些费劲。
    DateDiff(d, "month / day / year", now) 里面的d,怎么可以不加双引号?
    下面的代码,你可以参考一下:
    Sub aa()     Dim a1 As String 
        a1 = "06.05.20" 
         
        Dim date1 As Long 
        date1 = DateDiff("d", Replace(a1, ".", "-"), Now) 
        MsgBox date1 
    End Sub