[英]VBA: Nested Loops: sum columns within dataset
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; 循环似乎正确地进行了迭代,但是循环窗口“ a”中在即时窗口中输出的值不是我期望的; it seems to be referencing 1 cell in column A, 5x. 它似乎引用了A列中的1个单元格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: 数据样本,数据从第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
Assuming you want to sum each block of ten in each column and print that to the immediate window this code should work: 假设您想对每列中的每个十个块求和,并将其打印到立即窗口中,则此代码应该起作用:
Sub sumcols()
Dim blocks As Integer
Dim Fa As Long, Fb As Long, Fc As Long, Fd As Long, Fe As Long
Dim i As Long, j As Long
blocks = 9
For i = 2 To Sheets("sheet1").Range("A1048576").End(xlUp).Row Step blocks + 1 'A1048576' will need to be changed if excel version older than '07
'get sums for columns a through e for block starting with row i
Fa = Application.WorksheetFunction.Sum(Sheets("sheet1").Range("A" & i & ":A" & i + blocks))
Fb = Application.WorksheetFunction.Sum(Sheets("sheet1").Range("B" & i & ":B" & i + blocks))
Fc = Application.WorksheetFunction.Sum(Sheets("sheet1").Range("C" & i & ":C" & i + blocks))
Fd = Application.WorksheetFunction.Sum(Sheets("sheet1").Range("D" & i & ":D" & i + blocks))
Fe = Application.WorksheetFunction.Sum(Sheets("sheet1").Range("E" & i & ":E" & i + blocks))
'print values to immediate window
Debug.Print Fa
Debug.Print Fb
Debug.Print Fc
Debug.Print Fd
Debug.Print Fe
Next i
End Sub
Edit: 编辑:
to make the columns iterate as well use: 使列也可以迭代使用:
(Fa will return the sum for every column in this case) (在这种情况下,Fa将返回每一列的总和)
Sub sumcols()
Dim blocks As Integer
Dim Fa As Long, Fb As Long, Fc As Long, Fd As Long, Fe As Long
Dim i As Long, j As Long
Dim currange As Range
blocks = 9
For i = 2 To Sheets("sheet1").Range("A1048576").End(xlUp).Row Step blocks + 1 'A1048576' will need to be changed if excel version older than '07
'get sums for columns a through e for block starting with row i
For j = 1 To 5 'iterate through columns
'get range to sum (first range will be A2:A12)
Set currange = Sheets("sheet1").Range(Sheets("sheet1").Cells(i, j), Sheets("sheet1").Cells(i + blocks, j))
Fa = Application.WorksheetFunction.Sum(currange) 'Fa will return the sum for each column for each block
'print values to immediate window
Debug.Print Fa
Next j
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.