Skip to content

Instantly share code, notes, and snippets.

@ateneva
Last active April 22, 2017 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ateneva/5de9e43878ea0efd065179d92fb2f023 to your computer and use it in GitHub Desktop.
Save ateneva/5de9e43878ea0efd065179d92fb2f023 to your computer and use it in GitHub Desktop.
Modify the summary functions for each pivot table in your workbook
Sub ModifyDataFieldsSummaryFunction()
Dim PT As PivotTable
Dim PF As PivotField
Dim PI As PivotItem
Dim i As Integer
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'written by Angelina Teneva, 2014
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For i = 1 To 2
Worksheets(i).Activate
Set PT = ActiveSheet.PivotTables(1)
Select Case i
Case 1
ActiveSheet.name = Format(ActiveSheet.Range("K2"), "dd-mmm")
For Each PF In PT.DataFields
'must use DataFields Collection if you are going to change the method of consolidation
If PF.Position > 4 Then PF.Function = xlCountNums
If PF.Position <= 4 Then PF.Function = xlSum
If PF.Position <= 4 Then PF.NumberFormat = "0.0"
Next PF
Case 2
ActiveSheet.name = "weeks" & Format(ActiveSheet.Range("K2"), "dd-mmm")
For Each PF In PT.DataFields
'must use DataFields Collection if you are going to change the method of consolidation
If PF.Position > 3 Then PF.Function = xlCountNums
If PF.Position <= 3 Then PF.Function = xlSum
If PF.Position <= 3 Then PF.NumberFormat = "0.0"
Next PF
End Select
Next i
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment