+ Reply to Thread
Results 1 to 4 of 4

Macro to group sum of similar cells

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    96

    Macro to group sum of similar cells

    Hi

    Here is an example worksheet:

    Col A Col B
    A 2
    A 5
    B 1
    D 5
    A 2
    B 6
    A 5

    Now I am looking for a macro to group together all of the letters with the sum of each number beside that letter but this would be done in another sheet, so the above columns would still exist as they are on another sheet with the sorted elements separate. So, the outcome would be the following:

    Col A Col B
    A 14
    B 7
    D 5

    Hope I have explained things clear enough. Any help would be most appreciated.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Macro to group sum of similar cells

    Change the name of sheet 8 and should have a name sheet called Output sheet

    Sub jimmy()
    Dim x, i As Long, P As Long, k As Long, n As Long
    With Sheets("Sheet8").Range("a1").CurrentRegion
        x = .Value
    With CreateObject("Scripting.Dictionary")
        .comparemode = 1
        For i = 1 To UBound(x)
            If .exists(x(i, 1)) Then
                n = .Item(x(i, 1))
                 x(.Item(x(i, 1)), 2) = x(.Item(x(i, 1)), 2) + x(i, 2)
            Else
                P = P + 1
                .Item(x(i, 1)) = P
                For k = 1 To UBound(x, 2)
                x(P, k) = x(i, k)
                Next k
            End If
        Next i
    
    End With
        With Sheets("Output")
        .Cells.ClearContents
        .Range("A1").Resize(P, UBound(x, 2)).Value = x
        .Range("A1").CurrentRegion.Sort key1:=Range("A2"), order1:=xlAscending, _
                             Header:=xlNo, DataOption1:=xlSortTextAsNumbers
        .Activate
        End With
    End With
    Last edited by AB33; 02-27-2013 at 05:58 PM.

  3. #3
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Macro to group sum of similar cells

    Or you can use pivot table for that.

    The pivot table can also be added on the same sheet (if wanted).
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  4. #4
    Registered User
    Join Date
    04-02-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    96

    Re: Macro to group sum of similar cells

    Works perfect, thank you.

+ 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