Concatenate data from multiple cells in a row but grouping them based on duplicate values

Laura_10

New Member
Joined
Jul 15, 2012
Messages
16
Hi there,</SPAN>
I have to concatenate data from multiple cells in a row but grouping them based on duplicate values. </SPAN>
The sample data are in the table below between columns A and N, rows 1 to 5. </SPAN>
I have pairs of data in each row. The number of pairs could vary from 1 to 6. The pairs in row 1 are: H and x, D and y, C and x, F and z, H and y, Q and z, G and x. Data in columns A, C, E, G, I, K, M are unique in a row; data in columns B, D, F, H J, L , N could have duplicate values. Here is the sample:</SPAN>

A</SPAN>
B</SPAN>
C</SPAN>
D</SPAN>
E</SPAN>
F</SPAN>
G</SPAN>
H</SPAN>
I</SPAN>
J</SPAN>
K</SPAN>
L</SPAN>
M</SPAN>
N</SPAN>
Row1</SPAN>
H</SPAN>
x</SPAN>
D</SPAN>
y</SPAN>
C</SPAN>
x</SPAN>
F</SPAN>
z</SPAN>
H</SPAN>
y</SPAN>
Q</SPAN>
z</SPAN>
G</SPAN>
x</SPAN>
Row2</SPAN>
M</SPAN>
y</SPAN>
P</SPAN>
x</SPAN>
S</SPAN>
y</SPAN>
Q</SPAN>
y</SPAN>
D</SPAN>
x</SPAN>
Row3</SPAN>
J</SPAN>
v</SPAN>
E</SPAN>
v</SPAN>
W</SPAN>
v</SPAN>
Row4</SPAN>
P</SPAN>
z</SPAN>
C</SPAN>
z</SPAN>
N</SPAN>
x</SPAN>
T</SPAN>
z</SPAN>
Row5</SPAN>
E</SPAN>
x</SPAN>
O</SPAN>
x</SPAN>

<TBODY>
</TBODY>

I would like to concatenate all the cells in a row into a single cell, grouping them based on duplicate values in columns B, D, F, H J, L, N. The results should look like data in column O below:</SPAN>

Column O</SPAN>
Row1</SPAN>
H C G x, D H y, F Q z</SPAN>
Row2</SPAN>
M S Q y, P D x</SPAN>
Row3</SPAN>
J E W v</SPAN>
Row4</SPAN>
P C T z, N x</SPAN>
Row5</SPAN>
E O x</SPAN>

<TBODY>
</TBODY>


I need to find a solution in Excel to solve this problem.</SPAN>
Office 2010 and Windows 7 are on my computer.</SPAN>

Could someone help me. I much appreciate any ideas.</SPAN>

Thank you,</SPAN>
Laura_10</SPAN>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi Laura and welcome to Mr Excel Forum.

Maybe this UDF (User Defined Function)

Alt+F11 to open the VBEditor
go to Insert > Module
Paste the code below in the right-panel

Code:
Function ConcatCategories(r As Range) As String
    Dim i As Long, dict As Object, v As Variant
    Dim strResult As String
    
    If r.Columns.Count Mod 2 <> 0 Then _
        ConcatCategories = "ERROR - Range has an odd number of columns": Exit Function
    
    If r.Columns.Rows.Count > 1 Then _
        ConcatCategories = "ERROR - Range has more than 1 row": Exit Function
    
    Set dict = CreateObject("Scripting.Dictionary")
    
    For i = 1 To r.Columns.Count
        If r.Cells(i) = "" Then Exit For
        If i Mod 2 = 0 Then
            If dict.exists(r.Cells(i).Value) Then
                dict.Item(r.Cells(i).Value) = dict.Item(r.Cells(i).Value) & " " & r.Cells(i - 1).Value
            Else
                dict.Add r.Cells(i).Value, r.Cells(i - 1).Value
            End If
        End If
    Next i
    
    For Each v In dict.keys
        strResult = strResult & dict.Item(v) & " " & v & ", "
    Next v
    
    ConcatCategories = Left(strResult, Len(strResult) - 2)
End Function

Back to Excel

in O1
=ConcatCategories(A1:N1)

copy down

M.
 
Upvote 0
Hi Marcelo,

Thank you very much for your prompt solution.
I applied the steps you suggested in the mock spreadsheet I submitted in my question , columns A to N and results in column O. The results look perfect.

In my real spreadsheet the pairs of data are from columns BU to CN; there are 20 columns for the 10 pairs of data in each row. I loaded the UDF and I placed in column CO the formula "=ConcatCategories(BUx:CNx)". I dragged down the formula to cover all rows.
The concatenated result in column CO looks good. The only observation is I get a comma at the end of most of the concatenated results (i.e the result is like "E O x, ").

I don't know what is different in my real spreadsheet, comparing to the mock sample.

Thank you ,
Mirela
 
Upvote 0
Hi Mirela,

Could you post the data in a row where the result shows an extra comma?

Adjust the columns width, select the row, copy (ctrl+C) and paste in the forum reply page.

M.
 
Upvote 0
Mirela,

Maybe there are cells with spaces inside (not empty cells)

Try this new version

Code:
Function ConcatCatg(r As Range) As String
    Dim i As Long, dict As Object, v As Variant
    Dim strResult As String
    
    If r.Columns.Count Mod 2 <> 0 Then _
        ConcatCatg = "ERROR - Range has an odd number of columns": Exit Function
    
    If r.Columns.Rows.Count > 1 Then _
        ConcatCatg = "ERROR - Range has more than 1 row": Exit Function
    
    Set dict = CreateObject("Scripting.Dictionary")
    
    For i = 2 To r.Columns.Count Step 2
        If Trim(r.Cells(i)) = "" Then Exit For
        If dict.exists(r.Cells(i).Value) Then
            dict.Item(r.Cells(i).Value) = dict.Item(r.Cells(i).Value) & " " & r.Cells(i - 1).Value
        Else
            dict.Add r.Cells(i).Value, r.Cells(i - 1).Value
        End If
    Next i
    
    For Each v In dict.keys
        strResult = strResult & dict.Item(v) & " " & v & ", "
    Next v
    
    ConcatCatg = Left(strResult, Len(strResult) - 2)
End Function

CO1
=ConcatCatg(BU1:CN1)

copy down

M.
 
Upvote 0
Hi Marcelo,

Thank you very much for your replay.
You are right. There are cells with space inside.
I replaced the UFD with the new version you send me. The resulting data looks fine, no comma at the end of the data. But there are some cases when I get an error . It looks like the error is for lines without any data in columns BU to CN, or no data in column BV. I inserted 7 lines from my real spreadsheet, column BU to CN, and column CO with the results. The second line below has no data.
I hope you can see properly data below.

Thank you for help,
Mirela
[TABLE="width: 1531"]
<TBODY>[TR]
[TD]BSc(Hons)</SPAN>[/TD]
[TD]UNSW</SPAN>[/TD]
[TD]PhD</SPAN>[/TD]
[TD]ANU</SPAN>[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]BSc(Hons) UNSW,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TD] [/TD]
[TD]MA Well, PhD Tor</SPAN>[/TD]
[/TR]
[TR]
[TD]MSc</SPAN>[/TD]
[TD]Well</SPAN>[/TD]
[TD]PhD</SPAN>[/TD]
[TD]Well</SPAN>[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]MSc PhD Well</SPAN>[/TD]
[/TR]
[TR]
[TD]BA </SPAN>[/TD]
[TD]Well</SPAN>[/TD]
[TD]LLM</SPAN>[/TD]
[TD]Well</SPAN>[/TD]
[TD]DiplDrComp</SPAN>[/TD]
[TD]Stras</SPAN>[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]BA LLM Well, DiplDrComp Stras</SPAN>[/TD]
[/TR]
</TBODY><COLGROUP><COL span=3><COL><COL><COL><COL span=14><COL></COLGROUP>[/TABLE]
 
Upvote 0
Mirela,

Just for interest, since you're being already well looked after, I had a look at your original problem in Post#1.

Here's a VBA code. I just tested it on your post#1 but have no idea how or if it would work on your subsequent data.
Code:
Sub organise()
Dim rws As Long, i As Long, j As Long
Dim q As String, d As Object, e
Const Cls = "A:N"
rws = Range(Cls).Find("*", searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Row
Set d = CreateObject("scripting.dictionary")
a = Range(Cls).Resize(rws)

For i = 1 To rws
d.RemoveAll
q = vbNullString
For j = 2 To Range(Cls).Columns.Count Step 2
    If Len(a(i, j)) > 0 Then d(a(i, j)) = a(i, j)
Next j
For Each e In d.items
    For j = 1 To Range(Cls).Columns.Count Step 2
        If a(i, j + 1) = e Then q = q & " " & a(i, j)
    Next j
    q = q & " " & e & ", "
Next e

If Len(q) > 0 Then Cells(i, "o") = Mid(q, 2, Len(q) - 3)

Next i
End Sub
 
Upvote 0
Mirela,

Test Mirabeau's code. If it works without errors with your real data, great! Use it

Replying to #6
There is an easy workaround for the rows where there is no value in any cell.
We can wrap the function with an IF, like
CN01
=IF(COUNTA(BN1:CU1=0,"",ConcatCatg(BU1:CN1))
copy down

But i dont know an easy solution to the rows where there are blank cells on the 2nd (BV) or 4th(BX) or 6th(BZ)... columns and more data after these blank cells.

You have to insert some special code in these cells.

M.
 
Upvote 0
Hi Mirabeau,

Thank you for your help.
I ran the macro in my mock sample and the results look good. I copied and paste data from my real spreadsheet into the mock one and the results look good as well. In the mock spredasheet, data are between column A and N, the results are in column O.
In my real spreadsheet data are from column BU to CN, and the result of concatenation is in column CO; the lines start from line 5 down to 805. I made a small change to the macro replacing "A:N" with "BU:CN" and copied the macro in the real spreadsheet. I don't get any result in column CO after I ran the macro.
Can you please help me.
Thank you, Mirela
 
Upvote 0
Hi Marcel,

Than you for replay.
I entered the IF above and it solves the problem for rows without any value.
The main problem is coming form spaces which are in some of the cells.
Using the original UDF you sent me, I will do some test in Excel to remove the commas and spaces.
I will let you know what I get.

Thank you again for your help.
Mirela
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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