VBA to search for duplicates, concatenate cells from another column and delete row

JeremyG12

New Member
Joined
Feb 13, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have the below spreadsheet data that I need to determine if there are duplicate values in column A, if there are, then the values in column B (where a match exists) should be concatenated together and the duplicate row deleted. Note: the data that needs to be checked for duplicates does not start at the top of the column. It would be a range that needs to be checked for the duplicates (i.e., starting on Row 9 and going to Lastrow would work).

I've tried various approaches but haven't been able to get something working correctly. Appreciate the help!

Column AColumn B
other data
other details
Test 3abc, zzu, abf
Test 1jjff, jqqq
Test 3skjk
Test 4ksjs, kkskj
Test 3lok


Final output:
Column AColumn B
other data
other details
Test 3abc, zzu, abf, skjk, lok
Test 1jjff, jqqq
Test 4ksjs, kkskj
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Using Power Query:
Power Query:
let
    from = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    result = Table.Group(from, {"Column1"}, {{"Column2", each Text.Combine([Column2],", "), type text}})
in
    result
Book1.xlsm
AB
1Column1Column2
2other data
3other details
4Test 3abc, zzu, abf, skjk, lok
5Test 1jjff, jqqq
6Test 4ksjs, kkskj
Table2
 
Upvote 0
See if this works for you.
Use a copy of your workbook it will overwrite the existing data.

Change the sheet name to your sheet name

VBA Code:
Sub ConcatenateLines()

    Dim shtData As Worksheet
    Dim rowLast As Long
    Dim rngData As Range, arrData As Variant
    Dim dictData As Object, dictKey As String
    Dim i As Long
    
    Set shtData = Worksheets("Sheet1")          ' <--- Change to your sheet name or use ActiveSheet
    With shtData
        rowLast = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngData = .Range("A9:B" & rowLast)
        arrData = rngData.Value
    End With

    Set dictData = CreateObject("Scripting.dictionary")
    dictData.CompareMode = vbTextCompare
    
    ' Load details range into Dictionary
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 1)
        If Not dictData.exists(dictKey) Then
            dictData(dictKey) = arrData(i, 2)
        ElseIf dictData(dictKey) = "" Then
            dictData(dictKey) = arrData(i, 2)
        Else
            dictData(dictKey) = dictData(dictKey) & ", " & arrData(i, 2)
        End If
    Next i
    
    ' Write output
    rngData.ClearContents
    rngData.Columns(1).Resize(dictData.Count).Value = Application.Transpose(dictData.keys)
    rngData.Columns(2).Resize(dictData.Count).Value = Application.Transpose(dictData.items)
End Sub
 
Upvote 1
Solution
You’re welcome.
Just an FYI, transpose ha a limit of 65,536 lines, so if the number of rows in your resulting output is going to exceed that, come back to me.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top