如何比较(不同)工作表上的两个(透视)表数据?

时间:2014-03-20 11:28:29

标签: excel excel-vba excel-2010 pivot-table worksheet-function vba

1个excel文件中有2个工作表,具有以下相同的列结构:

BuildIndex | Phase | Module | Duration

BuildIndex用作主键

假设以下示例数据:

工作表1:

1 | Phase 1 | Module 1 | 5
1 | Phase 2 | Module 1 | 3
1 | Phase 3 | Module 1 | 10
1 | Phase 1 | Module 2 | 6
1 | Phase 2 | Module 2 | 2
1 | Phase 3 | Module 2 | 5

工作表2:

2 | Phase 1 | Module 1 | 3
2 | Phase 2 | Module 1 | 7
2 | Phase 3 | Module 1 | 9
2 | Phase 1 | Module 2 | 2
2 | Phase 2 | Module 2 | 10
2 | Phase 3 | Module 2 | 4

现在我创建不同的数据透视表和图表,并“手动”分析差异,以便做出像

这样的决策
  • 对于构建索引1 模块2 的构建速度比构建索引2
  • 快X秒
  • 用于构建索引2 阶段3 (所有模块的总和)构建比构建索引1

这就是我想要做的事情:

由于阶段很多且模块数量不断增加,上述过程需要花费太多时间,我认为有一种自动方式可以执行这样的分析。

那么,你有没有想过是否有办法实现我的意图?随意提供excel公式或数据透视表或vba或or的提示: - )

1 个答案:

答案 0 :(得分:0)

我用VBA解决了它。从来没有使用它,所以我的代码可以改进; - )

调用AllInOne阶段(使用的任何变量声明为String):

Option Explicit

Sub ExtractUniquePhasesAndModules()
    '--------------------------------------
    '| Perform calculations for TEST DATA |
    '--------------------------------------
    srcSheet = "CompareData"
    destSheet = "CompareResults"
    destPkColumn = "A"
    destColumn = "B"
    calculateColumn = "C"

    'Phases 1
    srcPkCell = "A2"
    srcColumn = "B"
    sumValuesColumn = "D"
    AllInOne srcSheet, srcColumn, destSheet, destColumn, calculateColumn, sumValuesColumn, srcPkCell, destPkColumn

    'Phases 2
    srcPkCell = "F2"
    srcColumn = "G"
    sumValuesColumn = "I"
    AllInOne srcSheet, srcColumn, destSheet, destColumn, calculateColumn, sumValuesColumn, srcPkCell, destPkColumn
End Sub

这是问题解决功能:

Private Sub AllInOne(srcSheetName As String, srcColumnName As String, destSheetName As String, _
                    destColumnName As String, calculateColumnName As String, sumValuesColumnName As String, _
                    srcPkCellName As String, destPkColumnName As String)

    Dim srcSheet As Worksheet
    Dim destSheet As Worksheet
    Dim srcColumn As Range
    Dim destColumn As Range
    Dim srcPkCell As Range
    Dim destPkColumn As Range
    Dim sumValuesColumn As Range
    Dim wsf As WorksheetFunction

    Set srcSheet = Worksheets(srcSheetName)
    Set srcColumn = srcSheet.Range(srcColumnName + ":" + srcColumnName)

    Set destSheet = Worksheets(destSheetName)
    Set destColumn = destSheet.Range(destColumnName + ":" + destColumnName)

    Set srcPkCell = srcSheet.Range(srcPkCellName)
    Set destPkColumn = destSheet.Range(destPkColumnName + ":" + destPkColumnName)

    Set sumValuesColumn = srcSheet.Range(sumValuesColumnName + ":" + sumValuesColumnName)

    Set wsf = WorksheetFunction

    '-----------------------
    'Copy all unique values|
    '-----------------------
    destSheet.Select
    Dim ctr As Range
    'find the first empty cell
    For Each ctr In destColumn.Cells
        If ctr.Value = "0" Then
            'do nothing
        ElseIf ctr.Value = Empty Then
            Exit For
        End If
    Next
    'start copying
    srcColumn.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=ctr, Unique:=True

    'set destination range to only the new cells
    Set destColumn = destSheet.Range(ctr.Address + ":" + destColumnName & destColumn.Count)

    Dim cell As Range
    Dim calcCell As Range
    Dim destPkCell As Range
    For Each cell In destColumn.Cells
        'end of list reached?
        If cell.Value = Empty Then
            Exit For
        End If

        'Fill in primary key
        Set destPkCell = destSheet.Range(destPkColumnName & cell.Row)
        destPkCell.Value = srcPkCell.Value

        'Perform the sum-calculation and show the result
        Set calcCell = destSheet.Range(calculateColumnName & cell.Row)
        calcCell.Value = wsf.SumProduct(wsf.SumIf(srcColumn, "=" & cell.Value, sumValuesColumn))
    Next

End Sub

首先,它遍历目标列以找到第一个空单元格。然后,此单元格在CopyToRange函数中用作AdvancedFilter参数。

然后它会插入主键(在我的情况下为BuildIndex),并为每一行插入SumProduct的结果。

使用问题数据的阶段结果如下:

1 | Phase   | 0
1 | Phase 1 | 11
1 | Phase 2 | 5
1 | Phase 3 | 15
2 | Phase   | 0
2 | Phase 1 | 5
2 | Phase 2 | 17
2 | Phase 3 | 13

现在我可以像我想要的那样创建图表: - )