0

I'm trying to calculate the average of the numbers of the third row for each category (a, b, c):

Data
enter image description here

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:
enter image description here

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
2
  • 1
    do you need a vba solution for a specific reason? because this can be easily solved with a Pivot Table or an AVERAGEIF function Commented Dec 3, 2020 at 20:20
  • 1
    I could fix this by making a pivot table, I know. The point is that I am trying to create an automation tool for work. The purpose of the sub was to select the first range and then offset 2 columns to the right. Your code is fine but I could not group averages by categories (a, b,c) Commented Dec 4, 2020 at 11:23

1 Answer 1

0

I also agree with Fernando and Michelle but I've made some changes to your first sub that may fix your issues:

Sub calculate_averages()

Dim r, rg1 As Range

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

Set rg1 = Selection.Offset(0, 2)

For each r in rg1
    
media = Application.Average(rg1)
MsgBox (media)

Next
           
End Sub

I'm not sure the purpose of your second sub. Is your intention to run both subs consecutively? If so they should probably just be one sub. And if this is the case there is a better way of doing all this than using .Selection and .Select, relying on these methods like this is dicey stuff.

Edit: My code may also not work properly, as I'm not sure what .Selection is... I assume this code is meant to run after a user has selected several rows in one column? If not, you need to rethink some more things. But the most glaring issue was that you didn't have any commands between For and Next

Sign up to request clarification or add additional context in comments.

2 Comments

I could fix this by making a pivot table, I know. The point is that I am trying to create an automation tool for work. The purpose of the sub was to select the first range and then offset 2 columns to the right. Your code is fine but I could not group averages by categories (a, b,c).
Pivot tables would be fine but so would be just basic worksheet functions. you are aware of =AVERAGEIF() ? If you want to make this into an automation, what kind of action do you anticipate the user taking to trigger it? Do you want them to select their data, then click a button? This is certainly doable using VBA but I don't understand the use case, i.e. how much of the process you want to automate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.