3

My excel sheet looks like this

      c1        c2      c3      c4

ROW1   abc      def      1       2

ROW2   abc      def      3       4

ROW3   klm      efg     11       5

ROW4   klm      efg     12      89

I want to combine the data into one single column separated by one comma for duplicate c1 entries. So excel sheet should look like this,

       c1        c2      c3      c4

ROW1   abc      def      1,3     2,4

ROW2   klm      efg     11,12    5,89
0

2 Answers 2

6

This code will

  • Run on columns A:D on the current sheet
  • Group records that are common in columns A and B togther with combined column C and column D values respectively
  • runs case insensitive
  • outputs to a new sheet

enter image description here

    Sub QuickCombine()
    Dim X()
    Dim Y()
    Dim objDic As Object
    Dim lngRow As Long
    Dim lngCol As Long
    Dim ws As Worksheet

    X = Range([a1], Cells(Rows.Count, "D").End(xlUp))
    Y = X
    Set objDic = CreateObject("scripting.dictionary")

    For lngRow = 1 To UBound(X, 1)
        If Not objDic.exists(LCase$(X(lngRow, 1) & X(lngRow, 2))) Then
            objDic.Add LCase$(X(lngRow, 1) & X(lngRow, 2)), lngRow
        Else
            Y(lngRow, 1) = vbNullString
            Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 3) = Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 3) & "," & X(lngRow, 3)
            Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 4) = Y(objDic.Item(LCase$(X(lngRow, 1) & X(lngRow, 2))), 4) & "," & X(lngRow, 4)
        End If
    Next

    Set ws = Sheets.Add

    ws.[a1].Resize(UBound(X, 1), UBound(X, 2)) = Y
    ws.Columns("A").SpecialCells(xlBlanks).EntireRow.Delete

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

+ 1 Nicely explained :) @Stuti: Brettdj was kind enough to give you everything on the plate. Show some efforts ;)
0

You can do this using the excel concatenate function. Here's a link to a good tutorial

Also, to deal with the duplicates, you can have excel highlight duplicate entries so they can be easily deleted. See here

1 Comment

I have 50000 entries in my sheet. I want to achieve this by a code which search for duplicate entries in c1 and combine entries in c3 and c4 till then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.