Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

EXCEL - Merge duplicate and append unique Cells

I have a listing of thousands of lines that are duplicate base off some unique primary cells.  There are some other cells that are unique.  I would like to append/combine these cells.  Is this possible?

I am attaching an example with the raw data and final output.  Not sure if Excel can do this or some VBA script can?

Example
0
holemania
Asked:
holemania
  • 2
1 Solution
 
holemaniaAuthor Commented:
The primary key/column would be A and B in this case.  A and B will be the same, but column C and D changes.  I want to remove duplicate and then just append column C and D with a comma separating them.
0
 
krishnakrkcCommented:
Option Explicit

Sub kTest()
    
    Dim dic As Object, i As Long, s As String
    Dim k, kk(), n As Long, c As Long
    
    Const SheetName As String = "Sheet2"      '<<<<<< adjust the sheet name
    
    Set dic = CreateObject("scripting.dictionary")
        dic.comparemode = 1
    
    With ThisWorkbook.Worksheets(SheetName)
        k = .Range("a1").CurrentRegion.Value2
        ReDim kk(1 To UBound(k, 1), 1 To UBound(k, 2))
        For i = 2 To UBound(k, 1)
            s = vbNullString
            s = k(i, 1) & "|" & k(i, 2) 'col a & b
            If Len(s) Then
                If Not dic.exists(s) Then
                    n = n + 1
                    For c = 1 To UBound(k, 2)
                        kk(n, c) = k(i, c)
                    Next
                    dic.Item(s) = n
                Else
                    c = dic.Item(s)
                    kk(c, 3) = kk(c, 3) & ", " & k(i, 3) 'update col c
                    kk(c, 4) = kk(c, 4) & ", " & k(i, 4) 'update col d
                End If
            End If
        Next
        If n Then
            .Range("a1").CurrentRegion.Offset(1).ClearContents
            .Range("a2").Resize(n, UBound(kk, 2)).Value = kk
        End If
    End With
    
End Sub

Open in new window


Kris
0
 
holemaniaAuthor Commented:
Awesome.  Works perfectly.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now