工作表函数sum.if中的Now()给出奇怪的结果

时间:2019-05-05 14:29:59

标签: excel vba

我有一些代码通过选择一些日期来使用工作表函数SumIf。当我使用日期函数Now()时,会得到奇怪的结果。

我想做的技巧是根据now()和其他日期之间的时间段计算som小计。但是,当我使用Now()时,我会得到奇怪的小计;而当我在遥远的将来(即“ 01-01-2900”)中使用“日期”时,我会得到正确的结果

此代码给出了奇怪的结果

With Worksheets(1).Range("B:B")
    Set c = .Find("STG PENSIOENFONDS ABP", LookIn:=xlValues, Searchorder:=xlByColumns)
    If Not c Is Nothing Then
        firstaddress = c.Address
       **arg2 = Now()**
        Do
            arg1 = CDbl(c.Offset(0, -1).Value)
            If Month(arg1) + 1 = 13 Then
                'MsgBox (Day(arg1) & "-" & Replace(Month(arg1) + 1, "13", "1") & "-" & Year(arg1) + 1)
                c.Offset(0, 6).Value = CDate(Day(arg1) & "-" & Replace(Month(arg1) + 1, "13", "1") & "-" & Year(arg1) + 1)
            Else
                c.Offset(0, 6).Value = CDate(Day(arg1) & "-" & Replace(Month(arg1) + 1, "13", "1") & "-" & Year(arg1))
            End If

            som1 = WorksheetFunction.SumIf(Range("A:A"), ">=" & CDbl(c.Offset(0, -1).Value), Range("G:G"))
            som2 = WorksheetFunction.SumIf(Range("A:A"), ">=" & arg2, Range("G:G"))
            som3 = som1 - som2
            c.Offset(0, 7) = som3
            arg2 = CDbl(c.Offset(0, -1).Value)
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstaddress
    End If

这是正确的

With Worksheets(1).Range("B:B")
    Set c = .Find("STG PENSIOENFONDS ABP", LookIn:=xlValues, Searchorder:=xlByColumns)
    If Not c Is Nothing Then
        firstaddress = c.Address
      **arg2 = "01-01-2919"**
        Do
            arg1 = CDbl(c.Offset(0, -1).Value)
            If Month(arg1) + 1 = 13 Then
                'MsgBox (Day(arg1) & "-" & Replace(Month(arg1) + 1, "13", "1") & "-" & Year(arg1) + 1)
                c.Offset(0, 6).Value = CDate(Day(arg1) & "-" & Replace(Month(arg1) + 1, "13", "1") & "-" & Year(arg1) + 1)
            Else
                c.Offset(0, 6).Value = CDate(Day(arg1) & "-" & Replace(Month(arg1) + 1, "13", "1") & "-" & Year(arg1))
            End If

            som1 = WorksheetFunction.SumIf(Range("A:A"), ">=" & CDbl(c.Offset(0, -1).Value), Range("G:G"))
            som2 = WorksheetFunction.SumIf(Range("A:A"), ">=" & arg2, Range("G:G"))
            som3 = som1 - som2
            c.Offset(0, 7) = som3
            arg2 = CDbl(c.Offset(0, -1).Value)
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstaddress
    End If

当我使用Now()时,subtotall是我不了解的数字,当我使用“ 01-01-2900”时,我得到了预期的subtallall

0 个答案:

没有答案