Solved

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

Posted on 2016-10-28
2
32 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 49

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

770 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