这应该适合你:
把它放在一个标准模块中(这是效率较低的方法 - 因为它检查所有字段):
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
然后,要在任何数据透视表编辑上自动运行此宏,您需要将其放入 Sheet1 代码中(如果您需要帮助,请告诉我)。
Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Call LinkPivotTables_ByFieldItemName_ToShowDetail(Target)
End Sub
如果所有数据透视表都在第一张表上,它将起作用。您可能必须先复制/粘贴到写字板或其他文本编辑器中,因为我不担心行长限制(注意自动换行)。
编辑/添加:
这是您将代码放在特定工作表对象上的方式:
EDIT2/ADDITION2 - 效率方法:
这种方法将显着提高效率,但您必须具体告诉它您要同步哪个字段(它不会全部同步):
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
您必须在此行中手动告诉它您要同步哪个字段:
PivotFieldIndex = "Year"
我在网上找到了其他几个使用相同循环方法来同步数据透视表的解决方案——问题是当你得到合适大小的数据透视表时,它们都会遇到相同的效率问题。通过包含一个在设置 Item.ShowDetail 值之前检查 Item.ShowDetail 值的 IF 语句,这些在某种程度上解决了这个问题(因为设置值比只检查它需要更长的时间)。祝你好运。