简体   繁体   English

评估求和积

[英]Evaluate sumproduct

I can't evaluate the sumproduct at the end of the code. 我无法在代码末尾评估sumproduct。 I think everything else is working but I keep getting the 我认为其他所有功能都可以使用,但我一直在

Type Mismatch Error 类型不匹配错误

I've tried all sorts of variations of syntax and I still can't get it to work. 我已经尝试了各种语法变体,但仍然无法正常工作。 Any ideas? 有任何想法吗?

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

rng1 and rng2 doesn't mean anything to Sumproduct function as it is not a Named Range or a valid range address. rng1rng2Sumproduct函数没有任何Sumproduct因为它不是Named Range或有效范围地址。 And so Evaluate function fails. 因此, Evaluate功能失败。

To make it work try: 要使其工作,请尝试:

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

Now to make sure that you evaluate your correct ranges, you might want to set the External argument of Address property to True . 现在,要确保您评估正确的范围,可能需要将Address属性的External参数设置为True

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM