How can I find and combine data based on key column.

trekker1218

Board Regular
Joined
Feb 15, 2018
Messages
86
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I have trying to combine multiple rows into columns based on key column item#. Example data below.
ABC
1
17122000002586
CE-KF81W/N1-210JDOBBA50002
2
17122000002586
JD0BBA50002
R0057W
3
17122000002586
14D581KM
86F809NG
4
15126
0344-00
050-0344-00
5
15126
950-0344-10
ESP-OL50EM26H
6
15126
512
ESPOL50EM26H
7
15126
ESP0L50EM26H
S4AO;50EM26H

I need to find all matching item#'s in COL A and result to display in a single row with each result in different column like result table below.
17122000002586CE-KF81W/N1-210JDOBBA50002JD0BBA50002
R0057W
14D581KM
86F809NG
151260344-00050-0344-00950-0344-10ESP-OL50EM26H512ESPOL50EM26HESP0L50EM26HS4AO;50EM26H

I have a worksheet with thousands of rows. So a formula or VBA is fine. which ever works best. I need to apply the formula to all of COL A and write results on a different Sheet.

Thanks in advance for all your help.
 

Attachments

  • 1591713350139.png
    1591713350139.png
    941 bytes · Views: 5
  • 1591713355272.png
    1591713355272.png
    941 bytes · Views: 4

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Here is your macro.
Paste it into a macro module. Read the comments. At the beginning there are two comments starting with <<<< . You are asked to check the sheet names there and modify as required. It is assumed the output sheet exists.

Then go to the output sheet, press Alt-F8 , select the macro and run it. Blink and you will miss the action

VBA Code:
Option Explicit

Sub CombineKeys()
    Dim wsIn As Worksheet, wsOut As Worksheet
    Dim vIn As Variant, vOut As Variant
    Dim lR1 As Long, lR2 As Long, lC1 As Long, lC2 As Long, UBi1 As Long, UBi2 As Long, UBo1 As Long, UBo2 As Long
    Dim colKey As Collection
    
    Set colKey = New Collection
    
    Set wsIn = Sheets("KeyIn")          '<<<< adjust sheet name to input sheet
    Set wsOut = Sheets("KeyOut")        '<<<< adjust sheet name to output sheet
    
    'read the input range into array for fast processing
    vIn = wsIn.Range("A1").CurrentRegion.Value
    'get array size
    UBi1 = UBound(vIn, 1)
    UBi2 = UBound(vIn, 2)
    
    
    'Get the unique keys. Do this by adding into collection.
    'When trying to add a key (2nd parameter) to collection, it will _
     error if the key already exists. By telling VBA to continue, the _
     add wil be aborted and the next item processed
    On Error Resume Next
    For lR1 = 1 To UBi1
        'keys are in 1st column of vIn
        colKey.Add vIn(lR1, 1), CStr(vIn(lR1, 1))
    Next lR1
    On Error GoTo 0     'reset error behaviour to give warning
    
    'make a first guess at the required size of the output array
    'divide total nr items by number of keys as average items /key
    lC2 = UBi1 * (UBi2 - 1) / colKey.Count
    lC2 = lC2 + 1
    If lC2 < UBi2 Then lC2 = UBi2
    
    ReDim vOut(1 To colKey.Count, 1 To lC2 + 1)
    UBo1 = colKey.Count
    UBo2 = lC2 + 1
    
    For lR2 = 1 To colKey.Count
        vOut(lR2, 1) = colKey.Item(lR2)         'get key in first column of output
        lC2 = 2
        For lR1 = 1 To UBi1
            If vOut(lR2, 1) = vIn(lR1, 1) Then  'same key, get items on that row
                For lC1 = 2 To UBi2
                    If Len(vIn(lR1, lC1)) Then
                        'add item to output array
                        vOut(lR2, lC2) = vIn(lR1, lC1)
                        ' increase column counter of output array
                        lC2 = lC2 + 1
                        If lC2 > UBo2 Then
                            'make the array larger to accept more items under the key
                            ReDim Preserve vOut(1 To colKey.Count, 1 To lC2 + 5)
                            UBo2 = UBound(vOut, 2)
                        End If
                    Else    'empty, go to next row of input array
                        Exit For
                    End If
                Next lC1
            End If
        Next lR1
    Next lR2
    
    'dump output array to output sheet
    wsOut.Range("A1").Resize(UBo1, UBo2).Value = vOut
    
    Set colKey = Nothing
    Set wsIn = Nothing
    Set wsOut = Nothing
End Sub
 
Upvote 0
Using Power Query, I brought your range into PQ, Unpivoted the data to allow for creating a Pivot Table. Brought the data back to excel and modified slightly the cell orientation. Here is the Mcode for the PQ portion.

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type any}, {"Column3", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Column1"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Index", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Index", {{"Index", type text}}, "en-US")[Index]), "Index", "Value")
in
    #"Pivoted Column"

Book8
ABCDEFGHIJ
1Column1012345678
2151260344-00050-0344-00950-0344-10ESP-OL50EM26H512ESPOL50EM26HESP0L50EM26HS4AO;50EM26H
317122000002586CE-KF81W/N1-210JDOBBA50002JD0BBA50002R0057W14D581KM86F809NG
Sheet2
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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