我在3个不同的标签上有3个不同的数据透视表。每个数据透视表都包含一个名为“Period”的字段(一列,因此每个Period都是一行)。每个“Period”都有多个“Ports”(同样是列)。然后我有Grand Total列 - 这只是每个时期“端口”的总和。
“期间”字段是动态的,随着时间的推移,期间可能会发生变化,一天可能有3个期间,下一个期间可能有10个期间(即今天深3行,明天深10行)。
每个数据透视表都以相同的方式呈现。 “Period”字段中的行可能在所有3个工作表中相同。例如:
Sheet 1中
期间..........总计
Sheet 2中
期间..........总计
表Sheet 3
期间..........总计
我想输出到第4张的范围,显示:
期间..........总计
随着总数的变化和周数的变化,应始终进行添加。我还想在必要时轻松添加更多数据透视表。希望足够清楚。
答案 0 :(得分:2)
嗯,下面应该是您尝试做的事情的基础。
请记住在参考文献中启用
Microsoft Scripting Runtime
。
这里我们加载一个包含所有包含枢轴的工作表的数组。然后我们遍历每个工作表,每个工作轴,然后是"期间的每个项目"领域。在各个枢轴上共同的周期字段的项目被添加到字典中。
一旦我们拥有了所有常见的字段项,我们就可以使用GetPivotData
循环回字典并对每个项的行总计求和 - 您可能需要更改传入此字段的字段名称代码的功能是为了你的目的。
然后我们打印出项目名称和总计 - 然后还打印不常见的项目。您可以修改代码以将此数据写入范围。
Option Explicit
Public Sub sum_gt_fields()
Dim wb As Workbook, ws As Worksheet
Dim pt As PivotTable, pt_itm As PivotItem
Dim dictCommon As Scripting.Dictionary, dictUncommon As Scripting.Dictionary
Dim k As Variant, i As Integer, j As Integer
Dim sum_gt() As Double
Dim pivot_sheet() As Variant
Set dictCommon = New Scripting.Dictionary
Set dictUncommon = New Scripting.Dictionary
'' Enter all sheets that contain pivots into the array
pivot_sheet = Array("pivot_sheet_1", _
"pivot_sheet_2")
Set wb = ThisWorkbook
For j = LBound(pivot_sheet, 1) To UBound(pivot_sheet, 1)
For Each pt In wb.Sheets(pivot_sheet(j)).PivotTables
'' May need to change "period" for actual row field name.
For Each pt_itm In pt.PivotFields("Period").PivotItems
'' Find out if there is the same column across pivotTables.
If Not dictUncommon.Exists(pt_itm.Name) Then
dictUncommon(pt_itm.Name) = 0
Else
dictUncommon.Remove pt_itm.Name
dictCommon(pt_itm.Name) = 0
End If
Next pt_itm
Next pt
Next j
'' Make array the size of how many common fields were found.
ReDim sum_gt(dictCommon.Count - 1)
'' Iterate over common field items; sum the grand totals.
For Each k In dictCommon.Keys
For j = LBound(pivot_sheet, 1) To UBound(pivot_sheet, 1) '' Add to iterate over sheets that have pivots
For Each pt In ws.PivotTables
'' You may need to change "ports" to the pt value field name used _
and "period" to the row field name used.
sum_gt(i) = sum_gt(i) + pt.GetPivotData("Ports", "Period", k)
Next pt
Next j
'' Print common field names and their summed totals.
'' You can use k and sum_gt(i) variables to output the data wherever you like.
'' At the moment it's printing to the immediate window (open with ctrl + G)
Debug.Print k & ": " & sum_gt(i)
i = i + 1
Next k
'' Print the fields that are not common across tables.
For Each k In dictUncommon.Keys
Debug.Print k
Next k
End Sub
打印出唯一的字段总计:
'' Print the fields and their grand totals, when they are unique.
Dim unique_gt() As Double
Dim gt As Double
ReDim unique_gt(dictUncommon.Count - 1)
i = 0
For Each k In dictUncommon.Keys
For j = LBound(pivot_sheet, 1) To UBound(pivot_sheet, 1)
For Each pt In wb.Sheets(pivot_sheet(j)).PivotTables
On Error Resume Next
gt = pt.GetPivotData("Ports", "Period", k)
If Err.Number = 0 Then
unique_gt(i) = gt
End If
On Error GoTo 0
Next pt
Next j
Debug.Print k & ": " & unique_gt(i)
i = i + 1
Next k