Concatenating Solution

jhumphrey22

New Member
Joined
Feb 6, 2014
Messages
1
Hello,

I have an issue whereby there are two columns of data that I need to essentially "roll-up". The first column is a list of IDs, and the second column is a list of names. The names listed are duplicated and are assigned to multiple IDs. The end goal is to have a new tab in an excel workbook that would now list the name in column A, and in column B show the IDs associated with that name separated by ", ". The idea is to have two views, one by ID with the corresponding names, and one by Name, with the IDs concatenated in the concurrent column.

This needs to be able to adapt to a varying numbers of rows, IDs and names. Can somebody help with this? At this point, we have to create a pivot table, find the IDs listed for each name, and then concatenate the IDs separated by ", ". It takes forever, as you can imagine.

Below is a sample file:
IDNAME
AS1234John Smith
AD1234Joe West
AF1234John Smith
AG1234Joe West
AK1234John Smith
AG1234Joe West
AR1234John Smith

<tbody>
</tbody>


Please help!!

Thanks,
JH
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
how about =B2 & "," & A2
 
Upvote 0
Hi and welcome to the forum,

Just out of interest, why do you need to concatenate the ID's?
Your PivotTable approach seems to make sense to me, and offers a nice/simple/filterable view of the IDs grouped by name?

Anyway, here is a procedure you can try (try it on a copy of your data):

Code:
Sub example()


    Dim vArr    As Variant
    Dim vOut    As Variant
    Dim v       As Variant
    Dim dict    As Object
    Dim i       As Long
    Dim j       As Long
    
    ' assign range to array
    vArr = Range("A1").CurrentRegion.Value2
    
    ' initialise dictionary
    Set dict = CreateObject("scripting.dictionary")
        
    With dict
        ' store distinct name values
        For i = 2 To UBound(vArr, 1)
            If Not .Exists(vArr(i, 2)) Then
                .Add Key:=vArr(i, 2), Item:=vArr(i, 1)
            Else
                .Item(vArr(i, 2)) = .Item(vArr(i, 2)) & ", " & vArr(i, 1)
            End If
        Next i
        
        ' populate output array
        ReDim vOut(1 To .Count, 1 To 2)
        For Each v In dict
            j = j + 1
            vOut(j, 1) = v
            vOut(j, 2) = .Item(v)
        Next v
    End With
    
    ' print column headers
    With Sheets("Sheet1").Range("D1")
        .Resize(1, 2).Value2 = Array("Name", "Concatenated IDs")
        .Resize(1, 2).Font.Bold = True
    End With
    
    ' print output array
    Sheets("Sheet1").Range("D2").Resize(UBound(vOut, 1), _
                                        UBound(vOut, 2)).Value2 = vOut


End Sub

This assumes your data table begins in A1 of Sheet1, and prints the output starting from D1 of Sheet1 (you can change the input and output ranges as required).
 
Upvote 0

Forum statistics

Threads
1,216,117
Messages
6,128,935
Members
449,480
Latest member
yesitisasport

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