Collating values from multiple rows into one row

emedley

New Member
Joined
Nov 17, 2015
Messages
23
I have just two columns of data (TABLE1) that I would like to covert (using functions) as shown in TABLE2

TABLE1 Col A lists Item numbers
Col B lists the Version of each Item in A

TABLE2 lists the Item numbers just once, but the Versions are collated in one cell, separated by a | character

TABLE1TABLE2
ItemVersionItemAll_Versions
d_40ad_40a|a.00|b|
d_40a.00d_41a|
d_40bd_42a.00|
d_41ad_43a.00|
d_42a.00d_44a.00|
d_43a.00d_45a|a.00|c|
d_44a.00d_46a.00|c|
d_45ad_47a|
d_45a.00d_48a|
d_45cd_49a|b|c|c.00|d|
d_46a.00d_50a|b|c|d|e|
d_46c
d_47a
d_48a
d_49a
d_49b
d_49c
d_49c.00
d_49d
d_50a
d_50b
d_50c
d_50d
d_50e

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>

Please could anyone help to provide a solution?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this VBA code:
Code:
Sub CreateNewList()

    Dim lr As Long
    Dim r As Long
    Dim itm As String
    Dim nxtitm As String
    Dim vers As String
    Dim newcl1 As String
    Dim newcl2 As String
    Dim nr As Long
    
'   Specify new columns to put new table in
    newcl1 = "D"
    newcl2 = "E"
    
    Application.ScreenUpdating = False
    
'   Put new column headers in
    Cells(1, newcl1) = "Item"
    Cells(1, newcl2) = "All Versions"
    
'   Find last row of data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows starting in row 2
    For r = 2 To lr
'       Get item and version values
        itm = Cells(r, "A")
        vers = vers & Cells(r, "B") & "|"
'       See if it differ the next item and write values to new line
        nxtitm = Cells(r + 1, "A")
        If itm <> nxtitm Then
            nr = Cells(Rows.Count, newcl1).End(xlUp).Row + 1
            Cells(nr, newcl1) = itm
            Cells(nr, newcl2) = vers
            vers = ""
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
It assumes that the original data is in columns A and B, with headers in row 1 and data starting on row 2.
You can change the destination columns ("D" and "E" in the code, if you like).
 
Upvote 0
Hi Joe4,

Thanks for your code!

It works as long as I keep Col A in order, but that's OK for me.
The code will be very useful as I'm going through >100k rows of data
 
Upvote 0
It works as long as I keep Col A in order, but that's OK for me.
Yes, I forgot to mention that assumption.

We could add a few extra lines of code to sort the data first, to make sure that happens, if desired.
 
Upvote 0
Here is another option for you to test. It doesn't require that the original table is sorted by the first column.
Code:
Sub All_Versions()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = d(a(i, 1)) & a(i, 2) & "|"
  Next i
  With Range("D1:E1")
    .Value = Array("Item", "All_Versions")
    .Offset(1).Resize(d.Count).Value = Application.Transpose(Array(d.Keys, d.Items))
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,873
Messages
6,127,454
Members
449,383
Latest member
DonnaRisso

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