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

#### victorsales

##### New Member
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?

#### Worf

##### Well-known Member
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
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
End If
Next
Target.AddDataField Target.PivotFields("Camp1"), "Sum of Camp1", xlSum
Application.EnableEvents = True
End Sub``````