pivot table divide help

kidrobot

Registered User.
Local time
Today, 16:14
Joined
Apr 16, 2007
Messages
409
If I have two fields for my data that are both SUM. Lets say SUM of cars and SUM of road. Is there any way that I can divide these for every record within the pivot table? So I want to know

SUM of cars/SUM of road


and have this populate maybe below each instance of the sums. If this is confusing let me know.
 
Howdy. Can you provide a small sample of dummy data, and then a mockup of what you want?
________
Jailbroken
 
Last edited:
Attached.

Right under

Sum of CountOfeqinit
Sum of SumOfLength

would it be possible to add a row that has them divided?
 

Attachments

Yes, that can be done. Select cell E6, then on the Pivot Table toolbar, on the dropdown on the far left (or top), where it says "Pivot Table", choose Formulas > Calculated field. In the resulting window, put a name for the field in the first box, in the one below that will have the formula, use the table fields below to select the appropriate fields, putting in the divisor sign (/) between them:

=SumOfLength/CountOfeqinit

Then click OK. Now it should have that division in the Pivot Table.
________
Honda Motocompo Specifications
 
Last edited:
One more little problem. I tried playing around with it for 30 min and I can't figure out why it isn't giving me the Average of "Sum of Field1" of each train. Any clue why?
 
This is where Pivot Tables can be frustrating. In fact, two years ago after reading and using chapter 12 of VBA and Macros for MS Excel by Bill Jelen and Tracy Syrstad, I have often used VBA for creating Pivot Tables and setting up formulas based on a copy of the Pivot Table, and final formatting (all in one macro). This gives you all the benefits of Pivots, plus the entire range of XL capabilities for just this kind of thing. And once you get the hang of it, VBA for Pivots is actually easier/faster than Pivots themselves. Bill and Tracy provide all the steps to get it done. Some of the code allows me to do this in less than 20 seconds - which is better than Pivot creation in XL, just by itself.
________
Buy iolite
 
Last edited:
So I guess you're suggesting using VBA. I know how to do certain VBA things in Access but not so much in XL. Do you by chance have an example of what I need?
 
I have modified example for several projects. It sets up a Pivot Cache, builds Pivot Table, copies Pivot data (PasteSpecial), then deletes the Pivot Table. Thus, you re-create the Pivot every time you run the code. The worksheet is called Data, and the Pivot Table is built on the same worksheet, offset two columns to the right of the data. Also, this code automatically adjusts for the size of the data (columns is set for 8 columns, but can also be setup like the rows to resize for new columns).

Code:
Sub CreateSummaryReportUsingPivot()
    ' Create Pivot Table (PT) for a summary report
    ' Region in the rows in PT and products as columns in PT

    Dim WSD As Worksheet
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    Dim PRange As Range
    Set WSD = Worksheets("Data")
    
    ' Delete any prior Pivot Tables
    For Each PT In WSD.PivotTables
        PT.TableRange2.Clear
    Next PT
    
    ' Define input area and set up a Pivot Cache
    FinalRow = WSD.Cells(65536, 1).End(xlUp).Row
    Set PRange = WSD.Cells(1, 1).Resize(FinalRow, 8)
    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange.Address)
        
    Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Range("J2"), TableName:="PivotTable1")
    PT.ManualUpdate = True
    ' Set up row fields
    PT.AddFields RowFields:="Region", ColumnFields:="Product"
    
    ' Set up data fields
    With PT.PivotFields("Revenue")
        .Orientation = xlDataField
        .Function = xlSum
        .Position = 1
    End With
    
    ' Code does not display total row or column
    With PT
        .ColumnGrand = False
        .RowGrand = False
        .NullString = "0"
    End With
    
    ' Calculate PT
    PT.ManualUpdate = False
    PT.ManualUpdate = True
        
    ' PT.TableRange2 contains the results. Move these to J10 (offset 2 col)
    ' as values, not a real PT.
    PT.TableRange2.Offset(1, 0).Copy
    WSD.Range("J10").PasteSpecial xlPasteValues
                
    ' Delete original PT & the Pivot Cache
    PT.TableRange2.Clear
    Set PTCache = Nothing

End Sub

I highly recommend getting the book VBA and Macros for MS Excel, Bill Jelen and Tracy Syrstad. They explain the code and offer other variations; this code is modified slightly from the book.
________
Vaporizer wholesale
 
Last edited:

Users who are viewing this thread

Back
Top Bottom