Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 29

Thread: Solved: Summarise Duplicate Help

  1. #1

    Question Solved: Summarise Duplicate Help

    Hi all,

    I recently obtained some code to summarise duplicate values in a range, whereby in a 2 column 2 row range, if the values in the first column where the same, the values in the second column would be added. (Incidentally, I have forgotten the author of this helpful code. My apologies to whoever you may be.)

    Anyhow, I was hoping someone could please assist in altering this code for me.

    First of all, rather than add all of the columns except column one, I was hoping to summarise only columns 5-7.

    And secondly, I was hoping to also check for duplicates in column 4. So if duplicates existing in the first column, the macro must then check column 4 for duplicates before summarising.

    I realise this may not make as much sense as I would like, so I have attached a simple example spreadsheet.

    Here is the code that I have at the moment. I would very grateful if someone can help.

    Thank you

    [VBA]Sub SummariseDuplicates()
    Dim cel As Range, rg1 As Range, rg2 As Range
    Dim ckValue As String, i As Long, j As Long
    Dim wb1 As Workbook, ws1 As Worksheet
    Dim vcels As Variant, ditem As Variant, vHeader As Variant

    Dim DuplicatesDic As Object
    Set DuplicatesDic = CreateObject("Scripting.Dictionary")

    Application.ScreenUpdating = False
    Set wb1 = ActiveWorkbook
    Set ws1 = ActiveSheet
    Set rg1 = Intersect(Selection.Columns(1).EntireColumn, ws1.UsedRange).Offset(1)
    vHeader = Intersect(ws1.Rows(1).EntireRow, ws1.UsedRange).Value
    For Each cel In rg1
    ckValue = LCase(cel)
    If ckValue <> "" Then
    Set rg2 = Intersect(cel.EntireRow, ws1.UsedRange)
    If Not DuplicatesDic.exists(ckValue) Then
    vcels = rg2
    DuplicatesDic.Add ckValue, vcels
    Else
    vcels = DuplicatesDic(ckValue)
    For i = LBound(vcels, 2) To UBound(vcels, 2)
    If IsNumeric(vcels(1, i)) And IsNumeric(rg2.Cells(i)) And i <> cel.Column Then
    vcels(1, i) = vcels(1, i) + rg2.Cells(i)
    End If
    Next i
    DuplicatesDic.Remove ckValue
    DuplicatesDic.Add ckValue, vcels
    End If
    End If
    Next cel
    ws1.Cells.Clear
    ws1.Range(Cells(1, 1), Cells(1, UBound(vHeader, 2))) = vHeader
    i = 1
    For Each ditem In DuplicatesDic.Items
    i = i + 1
    ws1.Range(Cells(i, 1), Cells(i, UBound(ditem, 2))) = ditem
    Next ditem
    Application.ScreenUpdating = True

    Set DuplicatesDic = Nothing
    End Sub[/VBA]
    Last edited by jacksonworld; 06-29-2006 at 07:32 PM.

  2. #2
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Sorry I over posted.
    Last edited by Shazam; 06-29-2006 at 07:00 PM.

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    I was thinking over and I think it could be done with a simple SUMIF function.

    Input formula in cell E10 copy down and across.

    =SUMIF($D$1:$D$3,$D10,E$1:E$3)

  4. #4

    There is an I in Idioit

    Thanks Shazam. Your answer has made me realise that I am an idiot in that I forgot to post my original code that requires changes. I apologise.

    [VBA]Sub SummariseDuplicates()
    Dim cel As Range, rg1 As Range, rg2 As Range
    Dim ckValue As String, i As Long, j As Long
    Dim wb1 As Workbook, ws1 As Worksheet
    Dim vcels As Variant, ditem As Variant, vHeader As Variant

    Dim DuplicatesDic As Object
    Set DuplicatesDic = CreateObject("Scripting.Dictionary")

    Application.ScreenUpdating = False
    Set wb1 = ActiveWorkbook
    Set ws1 = ActiveSheet
    Set rg1 = Intersect(Selection.Columns(1).EntireColumn, ws1.UsedRange).Offset(1)
    vHeader = Intersect(ws1.Rows(1).EntireRow, ws1.UsedRange).Value
    For Each cel In rg1
    ckValue = LCase(cel)
    If ckValue <> "" Then
    Set rg2 = Intersect(cel.EntireRow, ws1.UsedRange)
    If Not DuplicatesDic.exists(ckValue) Then
    vcels = rg2
    DuplicatesDic.Add ckValue, vcels
    Else
    vcels = DuplicatesDic(ckValue)
    For i = LBound(vcels, 2) To UBound(vcels, 2)
    If IsNumeric(vcels(1, i)) And IsNumeric(rg2.Cells(i)) And i <> cel.Column Then
    vcels(1, i) = vcels(1, i) + rg2.Cells(i)
    End If
    Next i
    DuplicatesDic.Remove ckValue
    DuplicatesDic.Add ckValue, vcels
    End If
    End If
    Next cel
    ws1.Cells.Clear
    ws1.Range(Cells(1, 1), Cells(1, UBound(vHeader, 2))) = vHeader
    i = 1
    For Each ditem In DuplicatesDic.Items
    i = i + 1
    ws1.Range(Cells(i, 1), Cells(i, UBound(ditem, 2))) = ditem
    Next ditem
    Application.ScreenUpdating = True

    Set DuplicatesDic = Nothing
    End Sub[/VBA]

  5. #5
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    For me it is not clear which column to be matched
    assuming Col.A to be matched..
    [vba]
    Sub test()
    Dim a, i As Long, ii As Integer, w(), y
    With ActiveSheet.UsedRange
    a = .Value
    .Clear
    End With
    With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 2 To UBound(a, 1)
    If Not IsEmpty(a(i, 1)) Then
    If Not .Exists(a(i, 1)) Then
    Redim w(1 To UBound(a, 2))
    For ii = 2 To UBound(a, 2)
    w(ii) = a(i, ii)
    Next
    .Add a(i, 1), w
    Else
    w = .item(a(i, 1))
    For ii = 2 To UBound(a, 2)
    If IsNumeric(a(i, ii)) Then w(ii) = w(ii) + a(i, ii)
    Next
    .item(a(i, 1)) = w
    End If
    End If
    Next
    y = .Items : Erase a
    End With
    With ActiveSheet.Range("a1")
    For i = 0 To UBound(y)
    .Offset(i).Resize(,UBound(y(i))) = y(i)
    Next
    End With
    Erase y
    End Sub
    [/vba]

  6. #6
    Thanks Jidon. Column A is the first column to check for duplicates. However, unfortunately the code you supplied did not work. Any ideas?

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Would probably help jindon if you stated what did not work about it.....did you get an error, if so what was the error. Did it not perform on the column you wanted it to, etc.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Good point. Thanks.

    I tried the code on the simple example I attached. It deleted the duplicates altogether and changed the figures in the non-dupicate row.

    Sorry if I am not explaining myself clearly. I am doing my best.

  9. #9
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    OK
    This is to summarize col.5-7(add) for unique iD col.A + col.D
    [vba]
    Sub test()
    Dim a, i As Long, ii As Integer, w(), y, z As String
    With ActiveSheet.UsedRange
    a = .Value
    .Clear
    End With
    With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 2 To UBound(a, 1)
    z = a(i, 1) & ";" & a(i, 4)
    If Len(z) >1 Then
    If Not .Exists(z) Then
    Redim w(1 To UBound(a, 2))
    For ii = 1 To UBound(a, 2)
    w(ii) = a(i, ii)
    Next
    .Add z, w
    Else
    w = .item(z)
    For ii = 5 To 7
    w(ii) = w(ii) + a(i, ii)
    Next
    .item(z) = w
    End If
    End If
    Next
    y = .Items : Erase a
    End With
    With ActiveSheet.Range("a1")
    For i = 0 To UBound(y)
    .Offset(i).Resize(,UBound(y(i))) = y(i)
    Next
    End With
    Erase y
    End Sub
    [/vba]
    Last edited by jindon; 07-03-2006 at 08:41 PM.

  10. #10
    Thanks for your persistence Jindon. I really do appreciate it.

    There is a minor error in the code, which I have unsuccessfully tried to fix.

    [VBA]z = a(i, 1) & ";" a(i, 4)[/VBA]

    How do I fix it?

    Thanks

  11. #11
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by jacksonworld
    Thanks for your persistence Jindon. I really do appreciate it.

    There is a minor error in the code, which I have unsuccessfully tried to fix.

    [vba]z = a(i, 1) & ";" a(i, 4)[/vba]

    How do I fix it?

    Thanks
    [vba]z = a(i, 1) & ";" & a(i, 4)[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  12. #12
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Quote Originally Posted by jacksonworld
    Thanks for your persistence Jindon. I really do appreciate it.

    There is a minor error in the code, which I have unsuccessfully tried to fix.

    [vba]z = a(i, 1) & ";" a(i, 4)[/vba]

    How do I fix it?

    Thanks
    Sorry should be
    z=a(i, 1) & ";" & a(i, 4)

  13. #13
    Thanks guys.

    I am receiving the Run-time error mesage "This key is associated with an element of this collection" for the following part of the code.

    [VBA].Add a(i, 1), w [/VBA]

    Sorry if this is taking a while.

  14. #14
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Sorry again..

    should be

     .add z, w
    and
    .item(z) = w
    Previous code has been modified, so copy entire code again and test it..

  15. #15
    Okey dokey. The duplicate is successfully being deleted, but unsuccessfully being summarised.

    The duplicates are correctly being condensed to one entry, however Columns E-G are not being summed for each column.

    The battle continues.

  16. #16
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Quote Originally Posted by jacksonworld
    Okey dokey. The duplicate is successfully being deleted, but unsuccessfully being summarised.

    The duplicates are correctly being condensed to one entry, however Columns E-G are not being summed for each column.

    The battle continues.
    No idea,,
    Are they numbers?

  17. #17
    Yeah, they are numbers. I am testing it on the example spreadsheet I attached at the beginning. I have attached it again.

    After the macro, the result should be the "answer" on Sheet 1.

    Thanks again.

  18. #18
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Unfortunately, I am currently unable to use excel with my PC,
    so I can not even test the code at the moment....
    If the code is running without error, it may be something to do with the cell format, not sure though
    try
    [vba]
    Sub test()
    Dim a, i As Long, ii As Integer, w(), y, z As String
    With ActiveSheet.UsedRange
    a = .Value
    .Clear
    End With
    With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 2 To UBound(a, 1)
    z = a(i, 1) & ";" & a(i, 4)
    If Len(z) >1 Then
    If Not .Exists(z) Then
    Redim w(1 To UBound(a, 2))
    For ii = 1 To UBound(a, 2)
    w(ii) = a(i, ii)
    Next
    .Add z, w
    Else
    w = .item(z)
    For ii = 5 To 7
    w(ii) = Val(w(ii)) + Val(a(i, ii))
    Next
    .item(z) = w
    End If
    End If
    Next
    y = .Items : Erase a
    End With
    With ActiveSheet.Range("a1")
    For i = 0 To UBound(y)
    .Offset(i).Resize(,UBound(y(i))) = y(i)
    Next
    End With
    Erase y
    End Sub
    [/vba]

  19. #19
    I'm impressed you are doing this without Excel.

    However, I am receiving the same result. I checked the cell format, but it is set to General.

    Puzzling.

  20. #20
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    OK

    Can you do the step debugging for me?

    1) Open vb editor and go to [View]-[LocaWindow]
    2) Click somewhere on the code then hit F8
    3) As you hit F8, the code execute line by line and you will see all the variables in LocalWindow
    4) if you come to the line of For ii = 5 To 7, Pleas watch into variable w
    which will be with + sign and if you click on the sign, you will see the progress as you hit F8
    5) Please check if the values w(5) to w(7) is changing or not

    rgds

Posting Permissions

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