Combine matches

liampog

Active Member
Joined
Aug 3, 2010
Messages
308
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

On Sheet1, I have the following columns of data

ABCD
1DATENUMBERNAMETYPE(S)
201/01/2019123J BloggsA,B
301/01/2019456D SmithA
401/01/2019789P JonesB,C
502/01/2019123J BloggsB
602/01/2019789P JonesD
703/01/2019456D SmithA,B,C
803/01/2019789P JonesC
903/01/2019000J DoeA
1003/01/2019123J BloggsA,C
1104/01/2019456D SmithA
1205/01/2019123J BloggsA,B
1305/01/2019456D SmithB,D

<tbody>
</tbody>


On Sheet2, I have the desired output shown below.

I have already created VBA that copies the B column from Sheet1 to Sheet2 and then removes duplicates to create a unique list of numbers. I then have a VLOOKUP set up to capture the name associated with the number. My problem is Column C on Sheet2.

I want to combine all of the Types from each customer, removing any duplicate entries.

The desired output is below and you can cross-reference this to Sheet1

ABC
1NUMBERNAMETYPE(S)
2123J BloggsA,B,C
3456D SmithA,B,C,D
4789P JonesB,C,D
5000J DoeA

<tbody>
</tbody>



Is there a formula that can achieve this? I'm sure there probably is but my knowledge of Excel formulas isn't amazing.

Thanks
 
How about the VBA code you provided prior? Editing it down so that it only focuses on extracting the Type data from Sheet1 that corresponds to the unique Number in Column A on Sheet2?

The VBA you provided works perfectly, but I don't need it to do anything about the Number and Name as this is all handled with VLOOKUPs.

I think I can create a Worksheet_Change event on Sheet1 that fires the code you gave whenever any Type data is changed.

I don't know anywhere near enough about VBA to be able to edit your VBA down to only do the above.

Thanks in advance
Liam

Rich (BB code):
Sub liampog()   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant
   Dim i As Long
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
      If Not Dic.Exists(Cl.Value) Then
         Dic.Add Cl.Value, Array(Cl.Offset(, 1).Value, CreateObject("system.collections.arraylist"))
      End If
      Ky = Split(Cl.Offset(, 2).Value, ",")
      For i = 0 To UBound(Ky)
         With Dic(Cl.Value)(1)
            If Not .contains(CStr(Ky(i))) Then .Add CStr(Ky(i))
         End With
      Next i
   Next Cl
   i = 1
   For Each Ky In Dic.Keys
      i = i + 1
      Dic(Ky)(1).Sort
      Sheets("sheet2").Range("A" & i).Resize(, 3).Value = Array(Ky, Dic(Ky)(0), Join(Dic(Ky)(1).toarray, ","))
   Next Ky
End Sub
 
Last edited:
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Surely if somebody adds more data to sheet1 you will need to rerun your existing macro incase there is a new number in col B. In which case you can simply run my initial code which will do everything.
 
Upvote 0
No it's not, but similar
Although I forgot a bit, use
Rich (BB code):
Sub liampog()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant
   Dim i As Long
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
      If Not Dic.Exists(Cl.Value) Then
         Dic.Add Cl.Value, Array(Cl.Offset(, 1).Value, CreateObject("system.collections.arraylist"))
      End If
      Ky = Split(Cl.Offset(, 2).Value, ",")
      For i = 0 To UBound(Ky)
         With Dic(Cl.Value)(1)
            If Not .contains(CStr(Ky(i))) Then .Add CStr(Ky(i))
         End With
      Next i
   Next Cl
   i = 1
   For Each Ky In Dic.Keys
      i = i + 1
      Dic(Ky)(1).Sort
      Sheets("sheet2").Range("A" & i).Resize(, 3).Value = Array(Ky, Dic(Ky)(0), Join(Dic(Ky)(1).toarray, ","))
   Next Ky
End Sub


Fluff

I used this solution in the end, however, is there any way of it just pulling the unique number in Column B of Sheet 1 and combining the unique values in Column D of Sheet 1 into Column A of Sheet2 and Column E respectively?

Thanks in advance.
Liam
 
Upvote 0
How about
Rich (BB code):
Sub liampog()
   Dim Cl As Range
   Dim Dic As Object
   Dim Ky As Variant
   Dim i As Long
   
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
      If Not Dic.Exists(Cl.Value) Then
         Dic.Add Cl.Value, CreateObject("system.collections.arraylist")
      End If
      Ky = Split(Cl.Offset(, 2).Value, ",")
      For i = 0 To UBound(Ky)
         With Dic(Cl.Value)
            If Not .contains(CStr(Ky(i))) Then .Add CStr(Ky(i))
         End With
      Next i
   Next Cl
   i = 1
   For Each Ky In Dic.keys
      i = i + 1
      Dic(Ky).Sort
      Sheets("sheet2").Range("A" & i).Value = Ky
      Sheets("sheet2").Range("E" & i).Value = Join(Dic(Ky).toarray, ",")
   Next Ky
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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