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.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top