Solved

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

Posted on 2016-10-28
2
22 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
2 Comments
 
LVL 48

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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now