VBA to auto rebuild of Pivot Table DataField collapse

birosd

New Member
Joined
Jun 17, 2016
Messages
6
I have been given the task to maintain a report with multiple pivot tables that summarize Data Source sheets that get updated weekly. The Data Sources have multiple column headers that change (e.g. Oct, Nov, Dec & 9/14, 9/21, 9/28). Thus, every time the pivot tables are refreshed, PivotFields get dropped and need to be re-added manually … to me a royal pain! Anyway, I have been trying to automate the “rebuild” of these pivot tables. I’m vba novice. I have code that will add a positioned PivotField (shown below), however the caption’s title has to be entered. I am looking for a way to pass the text in the data source column to the code that updates the pivot. I posted the version that works when typed in, and the version that does not work where I am trying to pass the text from the data source. I believe I am close, but I just cannot figure out why this approach is not working. Any help or ideas will be much appreciated.

Code that works:
Code:
Sub pivottable1()

   With ActiveSheet.PivotTables("PivotTable4").PivotFields("Sep Actual Hrs thru 9/25")
      .Orientation = xlDataField
      .Position = 2
   End With

End Sub

My attempt to retrieve the caption text from the data source (that does not currently work):
Code:
Sub PivotFieldAdd()

Dim pf As PivotField

Worksheets("Actual Data").Select   'name of the data source
'pf = Range("I3").Value

    'With ActiveSheet.PivotTables("PivotTable4").PivotFields("pf")    'this doesn't work either.
    With ActiveSheet.PivotTables("PivotTable4").PivotFields = Range("I3").Value
        .Orientation = xlDataField
        .Position = 2
   End With

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Like this:

Code:
Sub pivot_adjust()
Dim pt As PivotTable, pf$
Set pt = ActiveSheet.PivotTables("pivottable2")
pf = "Division"
'pf = ActiveSheet.[I3]           ' cell value
With pt.PivotFields(pf)
    .Orientation = 3
    .Position = 2
End With
End Sub
 
Upvote 0
(SOLVED) Thank you Worf! Using your example, I change a few things in my code and now it works perfectly! So now when the source data column heading changes, the pivot table is updated.

Code:
Sub PivotFieldAdd()
Dim pf As String
pf = Worksheets("Actual Data").[I3] 'name of the data source
'Selection.Copy
'pf = Range("I3").Copy
'MsgBox Worksheets("Actual Data").Range("I3").Value
   With ActiveSheet.PivotTables("PivotTable4").PivotFields(pf)    'this doesn't work either.
   'With ActiveSheet.PivotTables("PivotTable4").PivotFields = Range("I3").Value
        .Orientation = xlDataField
        .Position = 2
   End With
End Sub

:cool:
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top