VBA:嵌套循环:对数据集中的列求和

时间:2013-02-01 16:08:27

标签: excel-vba vba excel

我试图在数据集中对由列分隔的列内的值求和。循环似乎正在迭代,但是在即时窗口内输出的循环“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

1 个答案:

答案 0 :(得分:1)

假设您想要在每列中对每个十个块进行求和并将其打印到即时窗口,则此代码应该有效:

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

编辑:

使列迭代也使用:

(在这种情况下,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