I'm trying to calculate the average of the numbers of the third row for each category (a, b, c):
For instance: the average for all 'a', the average for all 'b', the average for all 'c'.
Then, I would like to insert three rows, one for 'a', one for 'b', one for 'c', with the average values on the third column and delete the old values. It would look like this:

Sub calculate_averages()
ActiveWorkbook.Worksheets("Sheet1").sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").sort.SortFields.Add2 Key:=Range("A2:A10") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").sort
.SetRange Range("A2:C10")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Selection.Offset(0, 2).Select
Set rg1 = Selection
n_rows = rg1.Rows.Count
For i = 1 To n_rows
Next
media = Application.Average(rg1)
MsgBox (media)
End Sub
Sub selecionar()
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
End Sub

AVERAGEIFfunction