Solved

Excel 2010- Group by Common Fields and Sum into Flat Tabulation with Unique Rows

Posted on 2013-05-11
6
235 Views
Last Modified: 2014-05-23
In Excel 2010 I need to have an automated method to produce a consolidated table of unique rows with sums on a number from another table that has many common columns (fields) but different quantities in the number field. A simple example with 2 fields to group by and 1 field to sum is as follows.

Source Tabulation
Row 1:    Knives            Kitchen Goods               6
Row 2:    Rags               Household Goods         4
Row 3:    Knives            Kitchen Goods               8
Row 4:    Rags               Household Goods         3

Target Tabulation
Row 1:    Knives            Kitchen Goods               14
Row 2:    Rags               Household Goods         7

Thank you,
Allen
0
Comment
Question by:amcgaughey1
  • 3
6 Comments
 
LVL 80

Expert Comment

by:byundt
Comment Utility
Allen,
What you are requesting is exactly the type of report that a PivotTable produces.

Although you can automate the production of a PivotTable, it is best if you first put your source data in a Table. In so doing, your PivotTable source will automatically include any added data. You'll still need to refresh the PivotTable to see the revised counts from new data, but that is a simple manual step (right-click the PivotTable, then choose Refresh). The refresh can also be automated if necessary.

Brad
0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
Code to create a Table (assumes that header labels exist and that cell A3 is in the data or header labels):
Sub CreateTable()
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$3").CurrentRegion, , xlYes).Name = "tbSource"
End Sub

Open in new window


Code to create a PivotTable on a new worksheet. Macro assumes that the active sheet contains a Table that will become the source of data for the PivotTable. It also assumes that the desired columns of information are first, second and third in that Table--but you can change that assumption in the commented section of the code.
Sub PivotTableCreator()
    Dim PTname As String, sGood As String, sType As String, sQuantity As String, TableName As String
    Dim lst As ListObject
    With ActiveSheet
        Set lst = .ListObjects(1)
        TableName = lst.Name                                'Name of Table
        sGood = lst.HeaderRowRange.Cells(1, 1).Value        'Name of the good is found in first column of Table
        sType = lst.HeaderRowRange.Cells(1, 2).Value        'Name of type of goods is found in second column of Table
        sQuantity = lst.HeaderRowRange.Cells(1, 3).Value    'Quantity of goods is found in third column of Table
        PTname = "PivotTable1"                              'Name of PivotTable to be created
    End With
    
    Sheets.Add after:=ActiveWorkbook.Worksheets(ActiveSheet.Index)
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=TableName).CreatePivotTable _
        TableDestination:=ActiveSheet.Name & "!R3C1", TableName:=PTname
    With ActiveSheet.PivotTables(PTname)
        With .PivotFields(sGood)
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields(sType)
            .Orientation = xlRowField
            .Position = 2
        End With
        .AddDataField .PivotFields(sQuantity), "Sum of " & sQuantity, xlSum
        With .PivotFields(sGood)
            .LayoutForm = xlTabular
            .RepeatLabels = True
            .Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
        End With
        .ColumnGrand = False
    End With
End Sub

Open in new window


Code to automatically refresh a PivotTable when the worksheet containing it is activated. This code must be installed on the code pane for the worksheet containing the PivotTable. It won't work at all if installed anywhere else!
Private Sub Worksheet_Activate()
Dim PT As PivotTable
For Each PT In Me.PivotTables
    PT.RefreshTable
Next
End Sub

Open in new window

Brad
PTautomationQ28125161.xlsm
0
 

Author Comment

by:amcgaughey1
Comment Utility
Thank you,
This soution works well for this small and specific tabulation, but it looks as if the code would need to be revised for tables that have additional columns.
I need the solution to be able to work on any size tabulation and incorporate changes to the column headers such as revising the name of 'Goods' to 'Category'.
Thoughts?
Allen
0
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
As posted, the code doesn't care how many columns of data you have in your source. Nor does it care what the header labels are on those columns. You didn't post a sample workbook, so I left comments in the code where you would need to make changes

You need to specify, however, either by name or number which columns are going to be used for the PivotTable. As written, the code assumes column numbers 1, 2 and 3 (the second index in .Cells in the three statements below. You will need to change those numbers to match your actual source data.
        sGood = lst.HeaderRowRange.Cells(1, 1).Value        'Name of the good is found in first column of Table
        sType = lst.HeaderRowRange.Cells(1, 2).Value        'Name of type of goods is found in second column of Table
        sQuantity = lst.HeaderRowRange.Cells(1, 3).Value    'Quantity of goods is found in third column of Table

Open in new window


Alternatively, you could specify the exact text of the exact header labels that you want to use. Then you would use that text in those same statements like this:
        sGood = "Goods"        'Exact text of header label for column containing goods
        sType = "Type"         'Exact text of header label for column containing type
        sQuantity = "Quantity"    'Exact text of header label for column containing quantity

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

762 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

9 Experts available now in Live!

Get 1:1 Help Now