Question

There are 2 worksheets in 1 excel file with the following identical column structure:

BuildIndex | Phase | Module | Duration

The column BuildIndex is used as primary key.

Assume the following example data:

Worksheet 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

Worksheet 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

For now I create different pivot tables and diagrams and analyze the differences "by hand" to make decisions like

  • for build index 1 the module 2 is build X seconds faster than in build index 2
  • for build index 2 the phase 3 (sum of all modules) is build Y seconds faster than in build index 1

That's what I want to do:

Because there are many phases and the count of modules is increasing continuously, the above procedure takes too much time and I think there's an automatic way to perform analyzes like these.

So, do you have any idea if there's a way to realize my intention? Feel free to provide hints for excel formulas or pivot tables or vba or or or :-)

Was it helpful?

Solution

I solved it using VBA. Never worked before with it, so my code could be improved ;-)

Call AllInOne for phases (any variable used is declared as 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

And this is the problem solving function:

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

First it iterates over the destination column to find the first empty cell. This cell is then used as CopyToRange argument in the AdvancedFilter function.

Then it inserts the primary key (BuildIndex in my case) and the result of SumProduct for every row.

The result for phases using the questions data is this:

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

Now I'm able to create diagrams just like I want :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top