Loop Adding Calculated Fields to Multiple Pivot Tables

MicaRunner

New Member
Joined
Dec 16, 2017
Messages
5
I'm trying to add two calced fields into 24 pivot tables across 8 worksheets. It it based on choice of a data validation list in B4 on Sheet 1.

The code pasted below works for all of them except for the last 5 tables on Sheet1. I have Field Headers disabled on all Pivot tables. The weird thing is if I enable field headers on the last 5 tables, the VBA works. It also still works if I enable field headers on any of the other tables.

For background, Sheet 1 has 12 pivot tables, Sheets 2-7 each have 1 and sheet 8 has 5. On the sheets with more than 1 pivot table, they are stacked vertically--as an example on sheet 1, PT1 starts in A5, PT2 in A15, PT3 in A25 and so on.
My question is why would the code run on some tables with or without field headers but only run on the 5 tables in question with field headers enabled? Full code is below:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
[/FONT][/COLOR][/FONT][/COLOR]
Dim sht As Worksheet
Dim pt As PivotTable
Dim pf As PivotField


'VBA only runs if cell B4 on the Summary tab is changed


On Error Resume Next
If Intersect(Target, Sheet1.Range("B4")) Is Nothing Then Exit Sub


'Removes values from Pivot Tables


    For Each sht In ThisWorkbook.Worksheets
        For Each pt In sht.PivotTables
            For Each pf In pt.DataFields
                pf.DataRange.Cells(1, 1).PivotItem.Visible = False
            Next pf
        Next pt
    Next sht


'When Hourly Utilization is chosen


    If Sheet1.Range("B4") = "Hourly Utilization" Then
        For Each sht In ThisWorkbook.Worksheets
            For Each pt In sht.PivotTables
                With sht.PivotTables(pt.Name)
                    .AddDataField sht.PivotTables(pt.Name).PivotFields("hClientUtil"), "Client"
                    .AddDataField sht.PivotTables(pt.Name).PivotFields("hProdUtil"), "Prod."
                    .DataBodyRange.NumberFormat = "#0.0%"
                End With
            Next pt
        Next sht
    End If


'When $ Weighted Utilization is chosen


    If Sheet1.Range("B4") = "$ Weighted Utilization" Then
        For Each sht In ThisWorkbook.Worksheets
            For Each pt In sht.PivotTables
                With sht.PivotTables(pt.Name)
                    .AddDataField sht.PivotTables(pt.Name).PivotFields("dClientUtil"), "Client"
                    .AddDataField sht.PivotTables(pt.Name).PivotFields("dProdUtil"), "Prod."
                    .DataBodyRange.NumberFormat = "#0.0%"
                End With
            Next pt
        Next sht
    End If

End Sub
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I realized that the 5 pivot tables that the code is not working on is because they only have one row of values so the pf.DataRange.Cells(1, 1).PivotItem.Visible = False selects the cell under the row of values. However, if I change the reference to Cells(0,1) to select the upper most left cell in the Data Range, the code filters out the first week of data (I have weeks running across the columns).
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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