评估产品

我不能在代码结束时评估sumproduct。 我认为其他一切都在工作,但我一直在努力

types不匹配错误

我已经尝试了各种各样的语法变体,但仍然无法正常工作。 有任何想法吗?

Sub Sample() Dim ws As Worksheet Dim x As Long Dim lRow As Long, llRow As Long Dim aCell As Range, bCell As Range Dim rng1 As Range, rng2 As Range Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set aCell = .Range("B6:E20").Find(Cells(14, 9).Offset(0, -1).Value) If Not aCell Is Nothing Then lRow = .Range(Split(.Cells(, aCell.Column).Address, "$")(1) & .Rows.Count).End(xlUp).Row If lRow > 1 Then Set rng1 = .Range(aCell.Offset(1), .Cells(lRow, aCell.Column)) End If End If Set bCell = .Range("B6:E20").Find(Cells(14, 9).Offset(-1, 0).Value) If Not bCell Is Nothing Then llRow = .Range(Split(.Cells(, bCell.Column).Address, "$")(1) & .Rows.Count).End(xlUp).Row If llRow > 1 Then Set rng2 = .Range(bCell.Offset(1), .Cells(lRow, bCell.Column)) End If End If Debug.Print rng1.Address Debug.Print rng2.Address x = Evaluate("=sumproduct(""rng1"",""rng2"")") End With End Sub 

rng1rng2Sumproduct函数没有任何Sumproduct因为它不是Named Range或有效范围地址。 所以Evaluatefunction失败。

要使其工作尝试:

 x = Evaluate("=sumproduct(" & rng1.Address & "," & rng2.Address & ")") 

现在为了确保评估正确的范围,可能需要将Address属性的External参数设置为True

 x = Evaluate("=Sumproduct(" & rng1.Address(, , , True) & _ "," & rng2.Address(, , , True) & ")")