将 Evaluate("SUMPRODUCT") 转换为 WorksheetFunction
Convert Evaluate("SUMPRODUCT") to WorksheetFunction
如何将此公式转换为具有分配范围的 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))")
非常感谢!
将公式转换为等效的 vba 工作表函数通常是可行的,但可能会很棘手,因为只有有限的函数子集可用。 (另见此处:Adding or multiplying variants in VBA)
根据给出的示例,我认为以下两个函数应该return相同的结果:
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
如何将此公式转换为具有分配范围的 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))")
非常感谢!
将公式转换为等效的 vba 工作表函数通常是可行的,但可能会很棘手,因为只有有限的函数子集可用。 (另见此处:Adding or multiplying variants in VBA)
根据给出的示例,我认为以下两个函数应该return相同的结果:
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