View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default calculate average from every two or three or four rows

Hi Turen,

Try this. I assumed that the source data is in column A I have set the
column number for the results to match the group to be averaged. That is if
averaging 2 cells then results in column 2, averaging 3 cells results in
column 3 etc. but you can edit this to place results wherever you like.

I assumed that you only need the principle for the programming and that you
can edit it from there to get as many groups of averages as you want but if
you want help to do more with it then feel free to get back to me.

Sub Averages()
Dim ws1 As Worksheet
Dim rng1 As Range
Dim i As Long
Dim aveGroup As Long
Dim colNbr As Long 'Column for results

Set ws1 = Sheets("Sheet1")
'Assign range to a variable
With ws1
Set rng1 = Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
End With

'Alternative methods of assigning range to a variable
'Set rng1 = ws1.Range("A2:A13")
'Set rng1 = Sheets("Sheet1").Range("A2:A13")

'set avegroup to the number of rows to average
aveGroup = 3

'Set colNbr for column to place results
colNbr = aveGroup

With rng1
For i = 1 To .Rows.Count Step aveGroup
ws1.Cells(Rows.Count, colNbr).End(xlUp).Offset(1, 0) _
= WorksheetFunction.Average(.Cells(i, 1), _
.Cells(i + aveGroup - 1, 1))
Next i
End With

End Sub


Regards,

OssieMac