我试图对数据集中由块分隔的列中的值求和。循环似乎正在正确迭代,但在即时窗口中输出的循环“a”中的值不是我所期望的;它似乎引用了 A 列中的 1 个单元格,5x。该脚本包含描述我想要实现的目标的注释。
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
数据样本,数据从第 2 行开始,不包括标题:
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