How to use a Pivot Table to sum the last 12 columns

victorsales

New Member
Joined
Nov 6, 2012
Messages
13
Hi Everyone,

hope you can help....as usual.

I have a pivot table based on an enormous database which essentially summarises each month points by individual.

I need to keep all historical columns in the pivot (this is going back to 2012) but would like the pivot to calculate automatically the sum of the last 12 columns as soon as new data is added to it.

I need the field to be inside the pivot because we need to sort from lowest to highest points. Adding a sum outside the pivot works but doesn't allow me to sort any value.

Any way I can accomplish this?
Many thanks for your help.
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
3,859
Hi

The example below sums the last two columns:

Excel Workbook
ABCD
3Values
4Row LabelsUnitsTotalSum of Camp1
5Andrews5925.474,196066,19
6Binder158788,42946,42
7Desk113.025,003036
8Pen56111,44167,44
9Pen Set2261.127,741353,74
10Pencil141421,59562,59
Order Report (2)

Code:
' sheet module
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim pf As PivotField, pfNew As PivotField, v, i%, c%, fs$
Application.EnableEvents = False
v = Split(Target.ColumnRange.Address, ":")
c = Range(v(1)).Column
fs = "="
For i = c - 1 To c - 2 Step -1                      ' build formula
    fs = fs & Cells(Range(v(1)).Row, i) & "+"
Next
fs = Left(fs, Len(fs) - 1)                          ' final formula
For Each pf In Target.CalculatedFields
    If pf.SourceName Like "*Camp*" Then             ' already exists
        pf.Delete
        Set pfNew = Target.CalculatedFields.Add("Camp1", fs)
    End If
Next
Target.AddDataField Target.PivotFields("Camp1"), "Sum of Camp1", xlSum
Application.EnableEvents = True
End Sub
 

Forum statistics

Threads
1,089,302
Messages
5,407,464
Members
403,143
Latest member
CTremblay

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top