Option Explicit
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'https://www.mrexcel.com/forum/excel-questions/1099164-pivot-calculation.html
'This code must be placed on the code page of the worksheet that contains
' the PivotTable it will run each time the PT is updated.
'Assuming the first cell of the last row of the DataBodyRange is the Gross
' Add % of Gross values below all cells in the row below the DBR
Dim rngCell As Range
Dim rngDBR As Range 'Databody Range
Dim rngGRT As Range 'Grand Total Range
Dim rngGross As Range
Dim lBorderIndex As Long
'Clear any cells formatted from prior run of this code. If you have
' formatted any cell interior color = 16777214 they will also be cleared
For Each rngCell In Me.UsedRange
If rngCell.Interior.Color = 16777214 Then
rngCell.Interior.Color = xlNone
rngCell.Borders.LineStyle = xlNone
End If
Next
Set rngDBR = ActiveSheet.PivotTables(1).DataBodyRange
Set rngGRT = rngDBR.Rows(rngDBR.Rows.Count)
Set rngGross = rngGRT.Cells(1, 1)
'Giving the cells where values will go a slightly offwhite color so they
' can be recognized and automatically deleted each time this code is run
With Union(rngGross.Offset(1, -1), rngGRT.Offset(1, 0))
.Interior.Color = 16777214
For lBorderIndex = 7 To 11 'Left, Top, Bottom, Right, Inside Vertical
With .Borders(lBorderIndex)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.14996795556505
.Weight = xlThin
End With
Next
End With
'Adding Row Name to right of Gross (First column in DBR)
rngGross.Offset(1, -1).Value = "Percentages"
'Adding values
For Each rngCell In rngGRT.Offset(1, 0).Cells
rngCell.Value = rngCell.Offset(-1, 0).Value / rngGross.Value
Next
'Formatting values
rngGRT.Offset(1, 0).Cells.NumberFormat = "0.00%"
End Sub