+ Reply to Thread
Results 1 to 6 of 6

Group data by two column with a sum on the third column

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    21

    Group data by two column with a sum on the third column

    I have this data set which needs to be grouped by data in the first two columns, but also with a sum within each grouping.

    The attached file has a before and after.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    860

    Re: Group data by two column with a sum on the third column

    Run the code and lookat sheets(2).
    Sub hsv()
    Dim chrtr, i As Long, cl As Range, sn As String
    Application.ScreenUpdating = False
    With Sheets(1)
      chrtr = Array("A", "B", "C", "D")
     
     With Sheets(2)
       .UsedRange.ClearContents
       .Range("A1").Resize(, 4) = chrtr
     End With
    For i = 0 To 3
    
    With .Range("A1:C" & .Cells(Rows.Count, 1).End(xlUp).Row)
        .AutoFilter 1, chrtr(i)
      For Each cl In Sheets(1).AutoFilter.Range.Offset(1).Columns(2).SpecialCells(12)
       If InStr(1, sn, cl, vbTextCompare) = 0 Then
         sn = sn & cl
    Sheets(2).Cells(Rows.Count, Columns(i + 1).Column).End(xlUp).Offset(1) = cl & " (" & WorksheetFunction.CountIf(.Columns(2).SpecialCells(2), cl) & ")"
         End If
        Next cl
      .AutoFilter
          End With
         sn = ""
       Next i
     End With
    End Sub
    Attached Files Attached Files
    Harry.

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Group data by two column with a sum on the third column

    Hi -

    Also try;
    Sub test()
    Dim rng, i
    rng = Range("a1").CurrentRegion
    With CreateObject("scripting.dictionary")
        For x = LBound(rng) + 1 To UBound(rng)
            If Not .exists(rng(x, 1)) Then
                .Add rng(x, 1), rng(x, 1)
            End If
        Next
        i = .items
        .RemoveAll
        For x = LBound(rng) + 1 To UBound(rng)
            If Not .exists(rng(x, 1) & "[]" & rng(x, 2)) Then
                .Add rng(x, 1) & "[]" & rng(x, 2), rng(x, 3)
            Else
                .Item(rng(x, 1) & "[]" & rng(x, 2)) = .Item(rng(x, 1) & "[]" & rng(x, 2)) + rng(x, 3)
            End If
        Next
        col = 7: rw = 2
        For Each rec In i
            For Each d In .keys
                If Left(d, InStr(d, "[]") - 1) = rec Then
                    Cells(rw, col) = Mid(d, InStr(d, "[]") + 2) & " (" & .Item(d) & ")"
                    tot = tot + .Item(d)
                rw = rw + 1
                End If
            Next
        Cells(1, col) = rec
        Cells(rw, col) = "Total " & tot
        col = col + 1: rw = 2: tot = 0
        Next
    End With
    Erase rng
    End Sub
    Regards,
    Event

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Group data by two column with a sum on the third column

    try
    Option Explicit
    
    Sub test()
        Dim a, i As Long, dic As Object
        Dim e, s, n As Long, myTotal As Double
        Set dic = CreateObject("Scripting.Dictionary")
        dic.CompareMode = 1
        With Range("a1").CurrentRegion
            a = .Value
            For i = 2 To UBound(a, 1)
                If Not dic.exists(a(i, 1)) Then
                    Set dic(a(i, 1)) = _
                    CreateObject("Scripting.Dictionary")
                    dic(a(i, 1)).CompareMode = 1
                End If
                dic(a(i, 1))(a(i, 2)) = dic(a(i, 1))(a(i, 2)) + a(i, 3)
            Next
            With .Offset(, .Columns.Count + 3)
                .CurrentRegion.ClearContents
                .Cells(1).Resize(, dic.Count).Value = dic.keys
                For Each e In dic
                    For Each s In dic(e)
                        myTotal = myTotal + Val(dic(e)(s))
                        dic(e)(s) = s & " (" & dic(e)(s) & ")"
                    Next
                    n = n + 1
                    .Cells(2, n).Resize(dic(e).Count).Value = _
                    Application.Transpose(dic(e).items)
                    .Cells(dic(e).Count + 2, n).Value = "Total " & myTotal
                    myTotal = 0
                Next
                With .CurrentRegion.Columns
                    .AutoFit
                    .HorizontalAlignment = xlCenter
                End With
            End With
        End With
    End Sub
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Group data by two column with a sum on the third column

    Hi Jindon -

    Nice approach

    much faster by 1.5 seconds running a test of 500000 records compared to mine.

    Regards,
    Event

  6. #6
    Registered User
    Join Date
    11-15-2012
    Location
    US
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Group data by two column with a sum on the third column

    Thank you to all...the solutions are great.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1