Originally Posted by
jtl
Try this out. Assuming that the word "Subtotal" is located in column 1:
[vba]
Sub sample()
Dim subtotalCol As Integer, averagecol As Integer
Dim firstRowtoAverage As Long, subtotalRow As Long
Dim x As Long, findtotal As Integer
subtotalCol = 2
averagecol = 3
findtotal = WorksheetFunction.CountIf(Columns(1), "Subtotal")
If findtotal <> 0 Then
firstRowtoAverage = 2
For x = 1 To findtotal
subtotalRow = Columns(1).Find(What:="Subtotal", After:=Cells(firstRowtoAverage, 1), SearchOrder:=xlByRows, SearchDirection:=xlNext).Row
Cells(subtotalRow, averagecol).FormulaR1C1 = "=average(R" & firstRowtoAverage & "C" & subtotalCol & ":R" & subtotalRow - 1 & "C" & subtotalCol & ")"
firstRowtoAverage = subtotalRow + 1
Next x
Else
MsgBox ("Subtotal does not exist")
End If
End Sub
[/vba]