JoeMontana
New Member
- Joined
- May 31, 2018
- Messages
- 4
Hi there,
I have a pivot table that has about 35 columns and I would like to quickly change all of the field settings (eg. from sum to max)
The macro that I have works in principle, but it crashes every time after two or three fields are changed. Any suggestions for improvements or workarounds?
The key part is the For Loop is:
My full script is:
I have a pivot table that has about 35 columns and I would like to quickly change all of the field settings (eg. from sum to max)
The macro that I have works in principle, but it crashes every time after two or three fields are changed. Any suggestions for improvements or workarounds?
The key part is the For Loop is:
Code:
For Each pf In pt.DataFields
pf.Function = xlCount
Next pf
My full script is:
Code:
Sub ChangeAllValueFields()
Dim pt As PivotTable
Dim pf As PivotField
Dim ws As Worksheet
Dim FieldSetting As String
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
FieldSetting = InputBox("Enter field setting for pivotchart" & vbCrLf & vbCrLf & "xlCount" & vbCrLf & "xlSum" & vbCrLf & "xlMin" & vbCrLf & "xlMax")
Application.ScreenUpdating = False
pt.ManualUpdate = True
If FieldSetting = "xlCount" Then
For Each pf In pt.DataFields
pf.Function = xlCount
Next pf
ElseIf FieldSetting = "xlSum" Then
For Each pf In pt.DataFields
pf.Function = xlSum
Next pf
ElseIf FieldSetting = "xlMin" Then
For Each pf In pt.DataFields
pf.Function = xlMin
Next pf
ElseIf FieldSetting = "xlMax" Then
For Each pf In pt.DataFields
pf.Function = xlMax
Next pf
Else
MsgBox ("Entry not on list. Changes cancelled")
End If
pt.ManualUpdate = False
Application.ScreenUpdating = True
Set pf = Nothing
Set pt = Nothing
Set ws = Nothing
End Sub
Last edited by a moderator: