Concatenate rows of data into a single cell based on a Unique ID

Sparkee

New Member
Joined
Aug 24, 2017
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a report coming out of a database that lists my data in rows, resulting in multiple lines for each person shown below. I would like to concatenate the data in the fruit and extra fruit columns into a single row for each person. I have had a play with TEXTJOIN and have ended up using UniqConcat. I have the below VBA code, and this is working to concatenate the rows, but I need it to re-set each time the Unique ID changes. I was hoping that one of you amazing people could assist me with this?

VBA Code:
Function UniqConcat(rng As Range, str As String)
Dim ucoll As New Collection, Value As Variant, temp As String

On Error Resume Next
For Each Value In rng
    If Len(Value) > 0 Then ucoll.Add Value, CStr(Value)
Next Value
On Error GoTo 0

For Each Value In ucoll
    temp = temp & Value & str
Next Value

temp = Mid(temp, 1, Len(temp) - Len(str))
UniqConcat = temp

End Function

]

My I want my spreadsheet looks like this so that I can then remove the duplicates and I am then left with one line per person.

Last NameFirst NameApplicationTypeExpiryDateFacilityFruitExtra FruitUnique IDResultExtra Result
SmithJamesNew
31/12/2020​
White HouseAppleChicken
803​
Apple, OrangeChicken, Beef, Lamb
SmithJamesNew
31/12/2020​
White HouseAppleBeef
803​
Apple, OrangeChicken, Beef, Lamb
SmithJamesNew
31/12/2020​
White HouseAppleLamb
803​
Apple, OrangeChicken, Beef, Lamb
SmithJamesNew
31/12/2020​
White HouseOrangeChicken
803​
Apple, OrangeChicken, Beef, Lamb
SmithJamesNew
31/12/2020​
White HouseOrangeBeef
803​
Apple, OrangeChicken, Beef, Lamb
SmithJamesNew
31/12/2020​
White HouseOrangeLamb
803​
Apple, OrangeChicken, Beef, Lamb
JonesLucyNew
31/12/2020​
White HousePlum
804​
Plum, Mango, Pear
JonesLucyNew
31/12/2020​
White HouseMango
804​
Plum, Mango, Pear
JonesLucyNew
31/12/2020​
White HousePear
804​
Plum, Mango, Pear
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Revised

Book3.xlsx
ABCDE
1
2
3Last NameFirst NameExpiryDateFruit2ExtraFruit2
4Jones
5Lucy
631/12/2020Plum,Mango,Pear
7Smith
8James
931/12/2020Apple,OrangeChicken,Beef,Lamb
10
11
Sheet3


used the amended formula to create new measures:

You can remove the duplicates by changing =CONCATENATEX(Table1,[Fruit],”, “) to =CONCATENATEX(Values(Table1[Fruit]),Table1[Fruit],”, “).

Of course you will have to create a new measure for Extra Fruit following the same methodology
 
Upvote 0
Ok, I have done it this way and it is working well. A bit time consuming but one it is set up, it will be easy to use.

thanks Alan.
 
Upvote 0
@Sparkee
What Excel version are you using?

I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using without having to ask the above question. The best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Thanks for the profile update. (y)

So, if your version of Excel 365 has the FILTER & UNIQUE functions (not everybody does yet but if not, you should have it soon) you may be able to use something like this that does not require you to subsequently remove duplicates.
The formula in column J needs to be entered in J2 only. The other ID values will automatically spill down to the other rows as required.
The formula in K2 is copied across, and down as far as you might ever need.

Sparkee 2020-07-14 1.xlsm
ABCDEFGHIJKL
1Last NameFirst NameApplicationTypeExpiryDateFacilityFruitExtra FruitUnique IDUnique IDResultExtra Result
2SmithJamesNew31/12/2020White HouseAppleChicken803803Apple, OrangeChicken, Beef, Lamb
3SmithJamesNew31/12/2020White HouseAppleBeef803804Plum, Mango, Pear 
4SmithJamesNew31/12/2020White HouseAppleLamb803  
5SmithJamesNew31/12/2020White HouseOrangeChicken803  
6SmithJamesNew31/12/2020White HouseOrangeBeef803  
7SmithJamesNew31/12/2020White HouseOrangeLamb803  
8JonesLucyNew31/12/2020White HousePlum804  
9JonesLucyNew31/12/2020White HouseMango804  
10JonesLucyNew31/12/2020White HousePear804  
Sheet1
Cell Formulas
RangeFormula
J2:J3J2=UNIQUE(H2:H10)
K2:L10K2=TEXTJOIN(", ",1,UNIQUE(FILTER(F$2:F$10,$H$2:$H$10=$J2,"")))
Dynamic array formulas.
 
Upvote 0
(y)Oh fantastic Peter, that works and is exactly what I needed. Thank you :)
 
Upvote 0
You're welcome. Thanks for the confirmation. :)
 
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,730
Members
449,333
Latest member
Adiadidas

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