我试图用多个标准得到总和:
- “获取”(A 列)
- “是”(C 栏)
- 求和列 K
从下图中可以看出,我设法在 Excel 中使用 SumProduct 得到答案。当我尝试使用 VBA 获得与 SumProduct 相同的答案时,我收到错误 13 类型不匹配。
图片:
有什么建议吗?
代码:
Option Explicit
Sub test()
Dim Criteria1 As String, Criteria2 As String
Dim Lastrow As Long, Result As Double
Dim rng1 As Range, rng2 As Range, rng3 As Range
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng1 = .Range("A3:A" & Lastrow)
Set rng2 = .Range("C3:C" & Lastrow)
Set rng3 = .Range("K3:K" & Lastrow)
Criteria1 = "Get"
Criteria2 = "Yes"
Result = Application.Evaluate("SumProduct(--(rng1 = Criteria1),--(rng2 = Criteria2),--rng3))")
End With
End Sub