You can also try this shorter version--
Sub average()
y = 1
On Error GoTo Err
Do While 1 = 1
x = Range("G" & y).Offset(1).End(xlDown).Row
y = Range("G" & x).End(xlDown).Row
Range("G" & x).End(xlDown).Offset(1).Value = WorksheetFunction.Sum(Range("G" & x & ":G" & y)) / (y - x + 1)
Range("H" & x).End(xlDown).Offset(1).Value = WorksheetFunction.Sum(Range("H" & x & ":H" & y)) / (y - x + 1)
Loop
Err:
End Sub
Just call this macro after inserting rows like this--
Sub InsertLine()
Dim lRow As Long
Dim Row As Long
For lRow = Cells(Cells.Rows.Count, "D").End(xlUp).Row To 2 Step -1
If Cells(lRow, "D") <> Cells(lRow - 1, "D") Then
Rows(lRow).EntireRow.Insert
Rows(lRow).EntireRow.Insert
Else
End If
Next lRow
Call average
End Sub
Bookmarks