为什么SumIf在Double中返回0并在VBA中返回带整数的true?

时间:2015-06-16 15:51:39

标签: excel vba excel-vba sumifs

下面是一个简单的SumIf计算,我希望sumif >=声明为k的变量Double

然而,返回0.如果我为任何整数,3,4等更改k,则返回true值。但如果我为k更改4.23983,则会返回0

我已经尝试声明另一个Double变量(比如说a)并放置a = sumif(...)。但这也给了我0。我也试过了"">="& k &""",但这也没有用(在Google上找到)。

Worksheets("ES").Cells(j + 10, 6).Value = Application.WorksheetFunction.SumIf(Worksheets("help").Range("A1:A4273"), ">=" & k, Worksheets("help").Range("A1:A4273"))

1 个答案:

答案 0 :(得分:0)

上次修改。根据评论,情况需要从欧洲格式转换为美国以进行计算。有关使用的大量代码,请参阅此帖子: VBA - Evaluate Conditions as String

这是一个可以添加到模块并在工作表中自由使用的函数:



    Function EuroSumIf(ByVal CellRange As Range, ByVal Condition As String, ByVal CompareVal As Double) As Double
        Dim cond As String

        Dim vx As MSScriptControl.ScriptControl
        Set vx = New MSScriptControl.ScriptControl
        Dim sum As Double

        For Each cell In CellRange.Cells
            a = cell.Value
            b = CompareVal
            If InStr(1, a, ".", vbTextCompare) > -1 Then
                a = Replace(a, ".", "")
            End If
            If InStr(1, a, ",", vbTextCompare) > -1 Then
                a = Replace(a, ",", ".")
            End If
            cond = "(a" & Condition & "b)"
            'MsgBox (cond & vbLf & a & Condition & b)
            With vx
                .Language = "VBScript"
                .AddCode "function stub(a,b): stub=" & cond & ": end function"
                If .Run("stub", a, b) Then
                    sum = sum + a
                End If
            End With
        Next
        EuroSumIf = sum
    End Function

使用如下:



    =EuroSumIf(A1:A3,">",0)