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