如何总结不同工作表上多个数据透视表的相应字段的行总计?

我有3个不同的选项卡上的多个3不同的数据透视表。 每个数据透视表包含一个名为“期间”(一列,因此每个期间是一行)的字段。 每个“期间”有多个“港口”(再次列)。 然后我有总共专栏 – 这是每个时期“港口”的总和。

“周期”字段是dynamic的,随着时间的推移,周期可能会改变,一天可能有3个周期,其次是10个周期(即今天3行,明天10行)。

每个数据透视表都以相同的方式呈现。 所有3个工作表中的“期间”字段中的行可能是相同的。 例:

工作表Sheet1

周期……….总计

  • 1月23 – 27日(总计= 10)
  • 1月30 – 03日(总计= 5)

Sheet2中

周期……….总计

  • 1月16 – 20日(总计= 15)
  • 1月23 – 27日(总计= 15)
  • 2月6 – 10日(总计= 5)

表Sheet 3

周期……….总计

  • 2月6 – 10日(总计= 10)

我想输出到一个范围,在第四张,显示:

周期……….总计

  • Jan 16-20:15
  • 1月23 – 27日:25
  • 1月30 – 03日:5
  • 2月6日至10日:15

随着总数变化和周数的变化,总是应该做的。 我也想在必要时轻松添加更多数据透视表。 希望是够清楚的。

那么,下面应该是你想要做的事情的基础。

请记住在引用中启用Microsoft Scripting Runtime

这里我们加载一个包含所有包含pivot的工作表的数组。 然后,我们循环遍历每个工作表,每个枢轴,然后遍历“期间”字段的每个项目。 字段中添加了枢纽中常见的句点字段项目。

一旦我们拥有了所有的常用字段项,我们就可以使用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