Solved

excel how to add group by average to bottom of the page

Posted on 2016-10-28
2
51 Views
Last Modified: 2016-11-08
I have excel sheet where I want to group by Location at bottom of page
ProductID, Product, Location, price
1     Product1, NY , 10
2     Product2, NY , 20
3     Product3, NJ , 10
3     Product3, NJ , 20
3     Product3, NJ , 30


Out put should be
ProductID, Product, Location, price
1     Product1, NY , 10
2     Product2, NY , 20
3     Product3, NJ , 10
4     Product3, NJ , 20
5     Product3, NJ , 30
      Average     NY   15    
      Average     NJ   20
     Grand Average          18
0
Comment
Question by:Bharat Guru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41864053
Hi,

pls try
Sub macro()
Dim Res() As Variant
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False

    For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
        If Not dict.exists(c.Offset(, 2).Value) Then
            dict.Add c.Offset(, 2).Value, c.Offset(, 2).Value
        End If
    Next

Res = dict.Items()
For Idx = 0 To UBound(Res)
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    Range("A" & lastRow).Offset(Idx + 1, 1) = "Average"
    Range("A" & lastRow).Offset(Idx + 1, 2) = Res(Idx)
    Range("A" & lastRow).Offset(Idx + 1, 3).FormulaR1C1 = "=averageIF(R2C3:R" & lastRow & "C3,RC[-1],R2C4:R" & lastRow & "C4)"
Next
Range("B" & Rows.Count).End(xlUp).Offset(1) = "Grand Average"
Range("D" & Rows.Count).End(xlUp).Offset(1).Formula = "=average(R2C4:R" & lastRow & "C4)"
Application.ScreenUpdating = True

End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:Bharat Guru
ID: 41879300
Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question