VBA SUM函数用于第n行中的多个值

时间:2018-08-17 10:10:09

标签: excel vba excel-vba excel-formula sum

我想问一下,当它们彼此分开30行时如何快速求和? 我想对9个值求和,并按照下面的代码在不同的列中输入结果:

Sub sum()
Range("EG12").Formula = "=Sum(C12,C282, C552, C822,C1092,C1362,C1632,C1902,C2172,C2442)"
Range("EG42").Formula = "=Sum(C42,C312,C582, C852,C1122,C1392,C1662,C1932,C2202,C2472)"
Range("EG72").Formula = "=Sum(C72,C342,C612, C882,C1152,C1422,C1692,C1962,C2232,C2502)"
Range("EG102").Formula = "=Sum(C102,C372,C642,C912,C1182,C1452,C1722,C1992,C2262,C2532)"
End Sub

您可能已经注意到,每个目标单元格彼此之间恰好位于30行(内部行为空或包含不同的值),同样地,复制的单元格位于恰好为270行之间(参见附件)。

enter image description here

我试图做这样的公式:

Sub sum2()
Dim lastrow As Long, i As Integer, total As Double, finalsum As Double
lastrow = Range("C2442").End(xlUp).Row

For i = 30 To lastrow
total = total + WorksheetFunction.sum(Range("C12" & i & "EG12" & i))
Next
finalsum = total

End Sub

...但是我遇到了“ object2_Global的方法2范围失败。

有人对此有想法吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

我用170的总和并没有使它变得那么漂亮,但是您应该理解这样的观点:

Sub test2()

Dim lastrow As Long, i As Long, finalsum As Long
lastrow = Range("C2442").End(xlUp).Row

finalsum = 0

For i = 12 To lastrow Step 30
Range("E" & i).Value = WorksheetFunction.Sum(Cells(i, 3).Value, Cells(i + 170, 3).Value, Cells(i + 2 * 170, 3).Value, Cells(i + 3 * 170, 3).Value, Cells(i + 4 * 170, 3).Value, Cells(i + 5 * 170, 3).Value, Cells(i + 6 * 170, 3).Value, Cells(i + 7 * 170, 3).Value, Cells(i + 8 * 170, 3).Value, Cells(i + 9 * 170, 3).Value)
finalsum = finalsum + Range("E" & i).Value
Next i


End Sub

答案 1 :(得分:1)

尝试使用循环和联合。

Option Explicit

Sub sum30by270()
    Dim i As Long, j As Long, lr as long
    Dim r1 As Range, r2 As Range

    With Worksheets("sheet2")
        lr = .cells(.rows.count, "C").end(xlup).row
        Set r1 = .Cells(12, "C")
        For i = 282 To lr Step 270
            Set r1 = Union(r1, .Cells(i, "C"))
        Next i
        'Debug.Print r1.Address(0, 0)

        Set r2 = .Cells(12, "EG")
        For j = 42 To 102 Step 30
            Set r2 = Union(r2, .Cells(j, "EG"))
        Next j
        'Debug.Print r2.Address(0, 0)

        r2.Formula = "=sum(" & r1.Address(0, 0) & ")"
    End With

End Sub

答案 2 :(得分:1)

少量评论:

lastrow = Range("C2442").End(xlUp).Row

用法不正确,您可能需要将其更改为:

'going from specified cell down until empty cell is met
lastrow = Range("C2442").End(xlDown).Row

'going from last cell in C column up, until first non-empty cell is met
lastrow = Range(Rows.Count, 3).End(xlUp).Row

第二个问题,如果要每30行循环一次,则应这样做(还要记住代码的正确缩进!):

For i = 12 To lastrow Step 30
    total = total + WorksheetFunction.Sum(Range(Cells(i, 1), Cells(i, 5)))
Next
finalsum = total

"C12" & i & "EG12" & i-&运算符是字符串连接,而不是加法运算符,这就是为什么您可能会得到意外结果的原因。

答案 3 :(得分:0)

好,谢谢大家对我们的贡献。我想为此问题添加最终的工作公式:

Sub sum_1to10()
Dim i As Long, j As Long, lr As Long
Dim r1 As Range, r2 As Range

With Worksheets("13")
lr = .Cells(.rows.Count, "C").End(xlUp).Row
Set r1 = .Cells(12, "C")                       'First cell with data in my worksheet - [![enter image description here][1]][1] C12
For i = 12 To 2442 Step 270                    ' From 1st cell with data to 10th cell in this order C2442
'When put "lr" instead 2442 the values will be calculated as per all worksheet data included (in my case it was down to 8377
    Set r1 = Union(r1, .Cells(i, "C"))
Next i
Set r2 = .Cells(12, "Eh")
For j = 12 To 1086 Step 30                    'First subsequent cell with data with 30 rows step e.g C42, C72, etc
' Value 1086 correspond to the last row in label with sum
    Set r2 = Union(r2, .Cells(j, "EG"))
Next j

r2.Formula = "=sum(" & r1.Address(0, 0) & ")"
End With
End Sub

希望我已经很好地理解了。 我决定修改此公式,以将我的计算分为三个部分。我每个月都会进行这些批量计算,并且已经将其划分了3年。