Im trying to sum the values within columns, which are separated by blocks, within a dataset. The loops seem to be iterating correctly but the values within the loop “a” that output within the immediate window are not what I expected; it seems to be referencing 1 cell in column A, 5x. The script contains comments describing what I’m trying to achieve.
Sub sumCells()
Dim i, r, a, numObs As Integer
Dim Fa, Fb, Fc, Fd, Fe As Long
numObs = 10
Dim subStart As Integer
'loop to separate data into "blocks" of 10 rows (numObs). Step function used
'to contain loops below within each block. starts at 2 due to headers
For subStart = 2 To sheet1.UsedRange.Rows.Count Step numObs
'loop to reference the rows within the subStart loop block
For r = 2 To numObs
'Loop to reference columns 1 through 5 (ideally I'd like to be able to
'have this reference the actual column index so i dont have to
'include more lines above to arrange the data accordingly
For a = 1 To 5
'this section is supposed to sum the values within each column
'in this case, columns 1 to 5, within the rows specified above
Fa = WorksheetFunction.Sum(Cells(r, 1))
Fb = WorksheetFunction.Sum(Cells(r, 2))
Fc = WorksheetFunction.Sum(Cells(r, 3))
Fd = WorksheetFunction.Sum(Cells(r, 4))
Fe = WorksheetFunction.Sum(Cells(r, 5))
Next a
Next r
Next subStart
End Sub
sample of the data, data starts on row 2, headers not included:
0 4 6 8 10 762 857 997
643 6 9 12 15 739 775 1084
784 8 12 16 20 938 828 968
1235 10 15 20 25 832 827 751
809 12 18 24 30 759 131 1085
1373 14 21 28 35 589 102 900
1300 16 24 32 40 656 798 962
901 18 27 36 45 782 812 786
761 20 30 40 50 706 951 741
Assuming you want to sum each block of ten in each column and print that to the immediate window this code should work:
Edit:
to make the columns iterate as well use:
(Fa will return the sum for every column in this case)