Check if forfields present in pivot table, then remove

RiaM

Board Regular
Joined
Jun 5, 2009
Messages
67
Hi guys

I've got a pivot table setup. The user can then play around with it i.e. drag and drop page fields into rowfields and columnfields and vice versa. Now I want to write a macro that checks if there are any rowfields or column fields present in the pivot table, and if so then relocate them to the page field area. The desired end result is that the pivot table is now empty (except for datafields). Any ideas?

I'm guessing its a loop to check if a rowfield is present, and if so relocate to the page field area. Same for column fields. Just now quite sure how to code this.

Blue skies!
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,325
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Something like:
Code:
   Dim pt As PivotTable, pf As PivotField
   Set pt = ActiveSheet.PivotTables(1)
   For Each pf In pt.RowFields
      pf.Orientation = xlPageField
   Next pf
   For Each pf In pt.ColumnFields
      pf.Orientation = xlPageField
   Next pf
should work.
 

RiaM

Board Regular
Joined
Jun 5, 2009
Messages
67
Hey Rory! Thanks, I'm starting to get the hang of this vb stuff :). Came up with the following just before I read your reply

Dim pf As PivotField, PT As PivotTable
Dim RowFieldName As String

Set PT = ActiveSheet.PivotTables("PivotTable1")

'Check if row field present, if not add it
For Each pf In PT.RowFields
If pf.SourceName <> "" Then
RowFieldName = pf.SourceName
With PT.PivotFields(RowFieldName)
.Orientation = xlPageField
.Position = 1
End With
End If
Next pf
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,325
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Not bad (;)), but the middle bit is unnecessary - pf is already a reference to the field, so you can just change its orientation directly.
 

RiaM

Board Regular
Joined
Jun 5, 2009
Messages
67
I'm trying to do the same thing with data fields. It works fine when there's no calcualted fields present, but if there are calculated fields present then it bombs out. I think this is because calculated fields cannot be turned off (xlhidden) but need to be deleted. Any ideas. The ffg code works for removing just the datafields:

Dim pf As PivotField, PT As PivotTable

Set PT = ActiveSheet.PivotTables("PivotTable1")

For Each pf In PT.DataFields
pf.Orientation = xlHidden
Next pf
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,325
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
Believe it or not, this is the only method I can get to work (as ludicrous as it seems):
Code:
Dim pf As PivotField, PT As PivotTable
Dim strOldName, strOldCalc
Application.ScreenUpdating = False
Set PT = ActiveSheet.PivotTables(1)
PT.ManualUpdate = True
For Each pf In PT.DataFields
   strOldName = pf.SourceName
   With PT.PivotFields(strOldName)
      If .IsCalculated Then
         strOldCalc = .StandardFormula
         .Delete
         PT.CalculatedFields.Add strOldName, strOldCalc
      Else
         pf.Orientation = xlHidden
      End If
   End With
Next pf
PT.ManualUpdate = False
Application.ScreenUpdating = True
 

Forum statistics

Threads
1,081,765
Messages
5,361,156
Members
400,615
Latest member
inzimam

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top