Reputation: 101
I want sum for columns. But the columns is each 3 columns, E.g. I want sum the column 4, column 7, column 10, ... column 48.
I'm use the function WorksheetFunction.Sum
For j = 1 To 54
For i = 3 To 48 Step 3
Cells(4 + j, 54) = WorksheetFunction.Sum(Cells(4 + j, i + 1))
Next i
Next j
But the result in every row is 0. How do I correct it?
Upvotes: 0
Views: 596
Reputation: 16174
Option Explicit
Sub summate()
Dim s As String, i As Long
For i = 7 To 49 Step 3
s = s & ",RC" & i
Next
Range("BB5:BB58").FormulaR1C1 = "=SUM(RC4" & s & ")"
End Sub
Upvotes: 1
Reputation: 49998
Something like the following could work:
Dim i As Long, j As Long, sum As Double
For j = 1 To 54
sum = 0 ' reset the sum for each iteration of the outer loop
For i = 3 To 48 Step 3
sum = sum + Cells(4 + j, i + 1).Value
Next i
Cells(4 + j, 54).Value = sum
Next j
Upvotes: 1