如何使用VBA管理sumproduct?

时间:2014-06-21 12:11:26

标签: excel-vba vba excel

我编写了以下代码:我正在尝试使用sumproduct函数和vba,但它不起作用。我尝试过使用evaluate函数但是我得到了同样糟糕的结果。

Dim row As Long
Dim rows As Range
Dim us As Integer
Dim rng As Range
Dim rngl As Range
Dim rngo As Range
Dim rngp As Range

With Sheets("Report")
    LastRow = .Range("C" & .rows.Count).End(xlUp).row
End With

Set rng = ThisWorkbook.Sheets("REPORT").Range("g3:g" & LastRow) 
Set rngl = ThisWorkbook.Sheets("REPORT").Range("F3:f" & LastRow)  

Set rngo = ThisWorkbook.Sheets("REPORT").Range("c3:c" & LastRow)  

For us = 3 To 33
    Range("e" & us).Value = Application.Evaluate("=SUMPRODUCT((sheets(luglio).Range(c & us))<rng)*(sheets(luglio).Range(c & us)=rngl)")
Next us

1 个答案:

答案 0 :(得分:1)

正如Rory在评论中提到的那样,这并不起作用,因为VBA将引号内的整个字符串传递给Excel。您需要连接字符串以获得所需的输出,如Excel公式中所示。

尝试替换该行:

 Range("e" & us).Value = Application.Evaluate("=SUMPRODUCT((sheets(luglio).Range(c & us))<rng)*(sheets(luglio).Range(c & us)=rngl)")

使用:

 Range("e" & us).Value = "=SUMPRODUCT((luglio!C" & us & "<REPORT!G3:G" & LastRow & ")*(luglio!C" & us & "=REPORT!F3:F" & LastRow & "))"