VBA for changing all field settings in a Pivot Table

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:
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:

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,397
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Welcome to the forum.

In what way does it crash? With an error message, or Excel freezing/crashing? Something else?
 

JoeMontana

New Member
Joined
May 31, 2018
Messages
4
Thanks Rory,

I'm a long time listener, first time caller :)

Excel crashes / stops working. Like it appears to be a memory issue.

The script will also run fully through without crashing if I step through each line. But then it only changes the first approx 10 fields. I don't know anything about the backend of excel but again, it seems like it just runs out of memory and stops executing the script.

I tried it with a smaller table, with 3 fields and it works fine.

Would adding a Wait line in the loop help?

I'm using 64-bit Office 365 ProPlus
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,397
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
You could try adding DoEvents to the loop:

Code:
    For Each pf In pt.DataFields
      pf.Function = xlMax
      DoEvents
    Next pf
 

JoeMontana

New Member
Joined
May 31, 2018
Messages
4

ADVERTISEMENT

Thanks Rory, that's a new command for me.

Unfortunately Excel still stops working if I try to run it or step into too quickly.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,397
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Is your pivot table using the data model?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,397
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
I wonder if that could be the issue. What happens if you don't use pt.manualupdate in the code?
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,550
Messages
5,512,021
Members
408,872
Latest member
Lorid24

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top