0

データセット内のブロックで区切られた列内の値を合計しようとしています。ループは正しく繰り返されているように見えますが、即時ウィンドウ内で出力されるループ「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
4

1 に答える 1

1

各列の 10 の各ブロックを合計し、それを即時ウィンドウに出力すると仮定すると、次のコードが機能するはずです。

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
于 2013-02-01T16:32:32.600 に答える