将Evaluate(“SUMPRODUCT”)转换为WorksheetFunction

时间:2015-01-12 16:19:50

标签: vba excel-vba excel-formula excel

如何将此公式转换为application.worksheetfunction.sumproduct并指定范围?

Set wb2 = ThisWorkbook)
Set ws1 = wb2.Sheets("b")
Set ws17 = wb2.Sheets("c")
Set ws18 = wb.Sheets("a")
Set ws19 = wb.Sheets("d")

ws18.range("A:A")

Evaluate("SUMPRODUCT((A:A=J2)*(E:E=F:F))") + _
'Evaluate("SUMPRODUCT((A:A=J2)*(E:E>F:F)*(F:F>0))")

非常感谢!

1 个答案:

答案 0 :(得分:0)

通常可以将公式转换为等效的vba工作表函数,但由于只有有限的函数子集可用,因此可能很棘手。 (另见:Adding or multiplying variants in VBA

根据给出的示例,我认为以下两个函数应该返回相同的结果:

Sub TestEvaluate()
With Sheet1
    x = .Evaluate("SUMPRODUCT((A1:A10=J2)*(E1:E10=F1:F10))")
    y = .Evaluate("SUMPRODUCT((A1:A10=J2)*(E1:E10>F1:F10)*(F1:F10>0))")
End With
Debug.Print x + y
End Sub

Sub TestWorksheetFunction()
With Sheet1
    a = .Range("A1:A10").Value2
    e = .Range("E1:E10").Value2
    f = .Range("F1:F10").Value2
    Set j = .Range("J2")
End With
With Application
    x = .SumProduct(.CountIf(j, a), .Delta(e, f))
    y = .SumProduct(.CountIf(j, a), .Delta(.GeStep(f, e)), .Delta(.GeStep(0, f)))
End With
Debug.Print x + y
End Sub