This should work for you:
Put this in a standard module(this is the less efficient method - because it checks all fields):
Sub LinkPivotTables_ByFieldItemName_ToShowDetail(pt As PivotTable)
Dim wkb As Workbook
Set wkb = ThisWorkbook
Dim wks As Worksheet
Set wks = wkb.Sheets(1)
Dim PivotTableIndex As Integer
Dim PivotFieldIndex As Integer
Dim PivotItemIndex As Integer
Dim PivotFieldIndexName As String
Dim PivotItemIndexName As String
Dim BoolValue As Boolean
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
For PivotFieldIndex = 1 To pt.PivotFields.Count
PivotFieldIndexName = pt.PivotFields(PivotFieldIndex).Name
For PivotItemsIndex = 1 To pt.PivotFields(PivotFieldIndex).PivotItems.Count
PivotItemIndexName = pt.PivotFields(PivotFieldIndex).PivotItems(PivotItemsIndex).Name
BoolValue = pt.PivotFields(PivotFieldIndex).PivotItems(PivotItemsIndex).ShowDetail
For PivotTableIndex = 1 To wks.PivotTables.Count
' This If statement will dramatically increase efficiency - because it takes a long long time to set the value but it doesn't take long to check it.
If wks.PivotTables(PivotTableIndex).PivotFields(PivotFieldIndexName).PivotItems(PivotItemIndexName).ShowDetail <> BoolValue Then
wks.PivotTables(PivotTableIndex).PivotFields(PivotFieldIndexName).PivotItems(PivotItemIndexName).ShowDetail = BoolValue
End If
Next PivotTableIndex
Next PivotItemsIndex
Next PivotFieldIndex
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
Then to automatically run this macro on any PivotTable edit you need to put this in your Sheet1 code(let me know if you need help doing that).
Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Call LinkPivotTables_ByFieldItemName_ToShowDetail(Target)
End Sub
It will work if all your pivot tables are on sheet one. You might have to copy/paste into wordpad or another text editor first because I didn't worry about line length limits(watch out for word wrap).
EDIT/ADDITION:
This is how you put code on a specific sheet object:
EDIT2/ADDITION2 - EFFICIENCY METHOD:
This method will dramatically increase the efficiency but you will have to tell it specifically which Field you want to be synced up(it won't sync them all):
Sub LinkPivotTables_ByFieldItemName_ToShowDetail(pt As PivotTable) 'takes as argument - pt As PivotTable
Dim wkb As Workbook
Set wkb = ThisWorkbook
Dim wks As Worksheet
Set wks = wkb.Sheets(1)
Dim PivotTableIndex As Integer
Dim PivotItemIndex As Integer
Dim PivotFieldIndex As String
Dim BoolValue As Boolean
Dim ItemName As String
Application.ScreenUpdating = False
Application.EnableEvents = False
PivotFieldIndex = "Year"
On Error Resume Next
For PivotItemsIndex = 1 To pt.PivotFields(PivotFieldIndex).PivotItems.Count
BoolValue = pt.PivotFields(PivotFieldIndex).PivotItems(PivotItemsIndex).ShowDetail
ItemName = pt.PivotFields(PivotFieldIndex).PivotItems(PivotItemsIndex).Name
For PivotTableIndex = 1 To wks.PivotTables.Count
' This If statement will dramatically increase efficiency - because it takes a long long time to set the value but it doesn't take long to check it.
If wks.PivotTables(PivotTableIndex).PivotFields(PivotFieldIndex).PivotItems(PivotItemsIndex).ShowDetail <> BoolValue Then
wks.PivotTables(PivotTableIndex).PivotFields(PivotFieldIndex).PivotItems(PivotItemsIndex).ShowDetail = BoolValue
End If
Next PivotTableIndex
Next PivotItemsIndex
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
You will have to manually tell it in this line what field you want synced up:
PivotFieldIndex = "Year"
I've found several other solutions online that use the same looping method to sync up pivot tables - the problem is that they all run into the same efficency problems when you get decent sized pivot tables. These somewhat get around that issue by including an IF statement that checks the Item.ShowDetail value before it sets it(because it takes alot longer to set the value than it does to just check it). Good Luck.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…