DavidJRuess

New Member
Joined
Feb 22, 2012
Messages
6
Hi Everyone,

I'm trying to come up with a simple macro that will scan through a list and calculate the average for a range that has a specific variable.
So it would go through the following table and give me the average for all the red's and then the average for the blue, and then the green, etc... (see table below).

Nothing is fixed. So I might have seventy five different Red numbers today and 45 red numbers tomorrow, sometimes there won't be any red numbers. And the numbers in column B will change each day as well.

I'm hoping to paste these results further down the sheet, or on another sheet (doesn't matter), where it lists the column A variable with the average next to it:
Red
60
Blue
77
etc...
etc..

<tbody>
</tbody>

Any help would be appreciative. I started trying to use a For, first row to last row, type of formula and using an if, to calculate for when the variable was only Red.

But then I started researching and figured there was a much simpler way to do it using Averageif, or an evaluate, or find method.

Thank you for your help :)

David




Column A
Column B
Red
15
Red
37
Red
132
Red
56
Blue
98
Blue
56
Green
24
Green
25
Green
100
Black
24
Black
34
Black
52
Black
78
Black
98
Yellow
67

<tbody>
</tbody>
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Test this out
Code:
Sub GetAverages()
    Dim rd As String, r As Double
    Dim bl As String, b As Double
    Dim gn As String, g As Double
    Dim bk As String, blk As Double
    Dim yw As String, y As Double
    Dim Rws1 As Long, Rng1 As Range
    Dim Rws2 As Long, Rng2 As Range


    Rws1 = Cells(Rows.Count, "A").End(xlUp).Row
    Set Rng1 = Range(Cells(1, 1), Cells(Rws1, 1))

    Rws2 = Cells(Rows.Count, "B").End(xlUp).Row
    Set Rng2 = Range(Cells(1, 2), Cells(Rws2, 2))

    rd = "Red"
    r = WorksheetFunction.AverageIfs(Rng2, Rng1, rd)
    
    bl = "Blue"
    b = WorksheetFunction.AverageIfs(Rng2, Rng1, bl)
    
    gn = "Green"
    g = WorksheetFunction.AverageIfs(Rng2, Rng1, gn)
    
    bk = "Black"
    blk = WorksheetFunction.AverageIfs(Rng2, Rng1, bk)
    
    yw = "Yellow"
    y = WorksheetFunction.AverageIfs(Rng2, Rng1, yw)
    
    Cells(Rows.Count, "A").End(xlUp).Offset(2, 0) = rd
    Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = r
    Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = bl
    Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = b
    Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = gn
    Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = g
    Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = bk
    Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = blk
    Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = yw
    Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = y

End Sub
 
Upvote 0
Thanks for the reply.

Will you help me understand what the code is actually doing? I'm a little confused by it, and when I try to modify it to my data it's not working properly.

Thanks
 
Upvote 0
Setup a summary table outside of your data with each of the colors you want to track and try something like this:

=averageif(A:A,G2,B:B)

Drag the formula down through your table and each cell should find the average of each item you entered in your summary table.

G2 is the cell where your color is in your summary table. Modify the formula accordingly.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top