为什么我会挂在使用SumProduct的这行代码上?

时间:2016-03-18 15:06:39

标签: excel vba excel-vba

出于某种原因,这部分代码挂起了“rng3.Value ...”行。请帮忙。

Set rng4 = ActiveSheet.Columns("D").Find(strFind, , xlValues, xlWhole)
    rng4.Select
    Set lastCell2 = ActiveSheet.Cells(ActiveSheet.Rows.Count, (4)).End(xlUp)
    lastCell2.Select
    Set rng5 = Range(rng4, lastCell2)
    rng5.Select
    Set rng5 = rng5.Offset(0, 0).Resize(rng5.Rows.Count - 1)
    rng5.Select
For i = 0 To 29
    Worksheets("Resource Plan").Select
    Set rng1 = ActiveSheet.Columns("J").Find(strFind, , xlValues, xlWhole)
    rng1.Select
    Set lastCell = ActiveSheet.Cells(ActiveSheet.Rows.Count, (10)).End(xlUp)
    lastCell.Select
    Set rng2 = Range(rng1, lastCell)
    Set rng2 = rng2.Offset(4, i).Resize(rng2.Rows.Count - 5, rng2.Columns.Count)
    rng2.Select
    Set rng3 = Sheets("Results").Cells(18, (i + 7))
    'rng3.Value = Worksheets("Resource Plan").Evaluate("Sum(," & rng2.Address(0, 0) & ")")
    rng3.Value = WorksheetFunction.SumProduct("rng5", "rng3")
    Worksheets("Results").Select
Next i

1 个答案:

答案 0 :(得分:0)

它们是变量,没有引号。同时调整rng5以删除标题。然后将其读入数组。目前你正在使用一个范围,而sumproduct需要一个数组。

Set rng5 = rng5.Offset(1, 0).Resize(rng5.Rows.Count - 1)
For Each cell In rng5
    i = i + 1
    aRng5(i) = cell.Value
Next cell

rng3.Value = WorksheetFunction.SumProduct(rng5, rng3)

主要问题似乎是数组目前的尺寸不匹配。您无法针对阵列产生单个单元格。如果你想用一个值多个每个单元格,你可能最终会循环遍历rng5然后将它们加在一起。在这种情况下,您不需要将其读入数组,但仍可以将其用于下一格式循环,乘法,然后将结果添加到变量中。如果第二个范围的大小应该匹配,那么你也需要在该范围内使用相同的下一个循环逻辑。