使用 VBA 比较 2 个数据透视表

Comparing 2 Pivot Tables using VBA

提问人:poojay 提问时间:10/18/2022 最后编辑:poojay 更新时间:10/18/2022 访问量:157

问:

我在同一个工作簿中有 2 个数据透视表。我现在正在尝试比较这两个表,以查找数据透视表 A 中每个表的行标签是否在数据透视表 B 中找到。如果它同时存在于两者中,那么我想比较同一行中的值,看看它们是否匹配。结果和备注将显示在同一本书的新工作表中。这是我试图完成的一个例子。我不知道是否有任何可行的方法,现在我正在尝试使用 2 for 循环,在两个数据透视表中循环。但是我不确定如何获取存在的行的单元格的值

表1

数据透视表 A

enter image description here

表2

数据透视表 B

enter image description here

输出表3

enter image description here

这是我试图做的事情的片段:

    Set brmSheet = Worksheets("PivotA")
    Set bscsSheet = Worksheets("PivotB")
    ' Print all the row labels
    Set pt = brmSheet.PivotTables("PivotTable1")
    Set pt2 = bscsSheet.PivotTables("PivotTable1")
    
    'Debug.Print pt.DataBodyRange.Rows.Count
    For i = 1 + 1 To pt.RowRange.Count - 1
        For j = 1 + 1 To pt2.RowRange.Count - 1
            If pt.RowRange.Cells(i).Value = pt2.RowRange.Cells(j).Value Then
                Debug.Print "Found " & pt.RowRange.Cells(i).Row & " : " & pt.RowRange.Cells(i).Value
            End If
            
        Next j
        
        'Debug.Print pt.RowRange.Cells(i).Row & ": " & pt.RowRange.Cells(i).Value
    Next i
    
Excel VBA 比较 数据透视表

评论

0赞 poojay 10/18/2022
我尝试这样做,但我不会工作 Set pt = brmSheet.PivotTables(“PivotTable1”) Set pt2 = bscsSheet.PivotTables(“PivotTable1”) 'Debug.Print pt.DataBodyRange.Rows.Count For i = 1 + 1 到 pt。RowRange.Count - 1 Debug.Print 点。RowRange(i) 下一个 i 错误。行范围 (i)

答:

0赞 IvanSTV 10/18/2022 #1

我这样做 - 创建 2 个数组,从每个透视数到数组读取数据并进行比较。你知道枢轴范围 - 所以枢轴不是动态范围?这很简单。

Dim a(),b(),count1 as Long,count2 as Long
count1=0'dimensiom of the first array
Do While Cells(count1+1,1)<>0if pivot b starts in 1 column
count1=count1+1
Loop
count2=0'dimensiom of the second array
Do While Cells(count2+1,6)<>0'if pivot b starts in 6 column
count2=count2+1
Loop
'filling the arrays 
ReDim a(count1,4)
Redim b(count2,4)
For i=2 to count1
For j=2 to 4
a(j,i)=cells(i,j)
Next j
Next i
ReDim a(count1,4)
Redim b(count2,4)

For i=2 to count2
For j=2 to 4
b(j,i)=cells(i,j+5)
Next j
Next i
'then comparing arrays
For i=1 to count1
For j=1 to count2
If a(3,i)=j(3,j) Then 'your code
.....
Next j
Next i

评论

0赞 poojay 10/18/2022
枢轴是动态范围。因此,枢轴列表比其他一些枢轴更长。但我不确定如何创建具有动态范围的数据透视表。我现在的代码是 ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ “BRM 数据!R2C1:R32330C39“,版本:=7)。CreatePivotTable TableDestination:= _ sheetName & “!R3C1“, TableName:=”PivotTable1“, DefaultVersion:=7
0赞 IvanSTV 10/18/2022
正如你所注意到的,第一个代码正在计算数据透视表的当前长度'Dim a(),b(),count1 as Long,count2 as Long count1=0'dimensiom of first array do While Cells(count1+1,1)<>0if pivot b start in 1 column count1=count1+1 Loop count2=0'dimensiom of second array Do While Cells(count2+1,6)<>0'if pivot b start in 6 column count2=count2+1 Loop'
0赞 IvanSTV 10/18/2022
TableDestination:= _ sheetName & “!数据透视的 R3C1 起点不是动态的 - 您知道数据透视的起点范围,您知道数据透视表列的数量,并通过第一个数据透视列计算每个数据透视的长度 - 然后将数据收集到数组中并进行比较
0赞 poojay 10/18/2022
因此,根据您给出的示例,我应该将所有行、列数据保存到每个透视的数组中。然后比较它们?
0赞 poojay 10/18/2022
是的,我知道它不是动态的。不确定如何创建具有动态 # 行/列的数据透视表