combine data from same source in 1 cell

chornat

New Member
Joined
Mar 16, 2021
Messages
8
Office Version
  1. 2010
Platform
  1. Windows
Hi,
I post some kind of the same question last week. But still has not get the final answer which I think my question may be too complicated so I post it again with simplify question.

Basically, I have data of same person in different row. How can I combine it in to one row or one cell? Thank you! ^-^

my data looks like this
Name
Preference
Nat​
Apple​
Nat​
Orange​
May​
Melon​
May​
Ovocado​
Jack​
Banana​
Jack​
Papaya​

I want it to look like this
Name
Preference
Nat​
Apple
Orange​
May​
Melong
Avocado​
Jack​
Banana
Papaya​
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Well let's get the ball rolling. Now this program will work. I know this should start the conversation going. Maybe some of the A students will weigh in and give us some more solutions.

VBA Code:
Sub Consol()

    Dim Name As String
    Dim Pref As String
    Dim LastRow As Integer
    Dim Row1 As Integer
    
    Row1 = 2
    LastRow = Range("A1", Range("A1").End(4)).Rows.Count
    
    For i = 2 To LastRow Step 2
    
    Cells(Row1, 4) = Cells(i, 1).Value
    Cells(Row1, 5) = Cells(Row1, 2) & vbNewLine & Cells(Row1 + 1 + 1, 2)
    Row1 = Row1 + 1
    
    Next i
    
End Sub

Book1
ABCDE
1NamePreference
2NatAppleNatApple Melon
3NatOrangeMayOrange Ovocado
4MayMelonJackMelon Banana
5MayOvocado
6JackBanana
7JackPapaya
Sheet4
 
Upvote 0
Hi Chornat,

I can't see how to put all in the same cell but I can repeat in the same row but multiple columns:

Chornat.xlsx
ABCDEFGHI
1NamePreferenceNamePreferences2nd3rd4th5th
2NatAppleNatAppleOrange   
3NatOrangeMayMelonOvocado   
4MayMelonJackBananaPapaya   
5MayOvocado      
6JackBanana      
7JackPapaya      
8      
9      
Sheet1
Cell Formulas
RangeFormula
D2:D9D2=INDEX($A$2:$A$999,MATCH(0,INDEX(COUNTIF($D$1:$D1,$A$2:$A$999),),0))&""
E2:I9E2=IF($D2="","",IFERROR(INDEX($B$2:$B$999,AGGREGATE(15,6,ROW($B$2:$B$999)-ROW($B$1)/($A$2:$A$999=$D2),COLUMN()-COLUMN($D$1))),""))
 
Upvote 0
I post some kind of the same question last week.
In future, please do not start a new thread in that circumstance. Refer to #12 of the Forum Rules and points 6 & 7 of the Guidelines.

For reference the other thread, which I have now locked, is here.

From that other thread it appears that items for one person may be repeated in the second column but you do not want them repeated in the results. If that is so, then you might try this macro.

VBA Code:
Sub CombinePrefs()
  Dim d As Object
  Dim a As Variant, b 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)
    If Not d.exists(a(i, 1)) Then d.Add a(i, 1), CreateObject("Scripting.Dictionary")
    d(a(i, 1))(a(i, 2)) = Empty
  Next i
  ReDim b(1 To d.Count, 1 To 2)
  For i = 1 To d.Count
    b(i, 1) = d.keys()(i - 1)
    b(i, 2) = Join(d.items()(i - 1).keys(), vbLf)
  Next i
  With Range("D2:E2").Resize(d.Count)
    .Value = b
    .Columns(2).WrapText = True
    .Rows(0).Value = Array("Name", "Preferences")
  End With
End Sub

My sample data and results:

chornat.xlsm
ABCDE
1NamePreferenceNamePreferences
2NatAppleNatApple Orange
3NatOrangeMayMelon Ovocado
4NatAppleJackBanana Papaya
5MayMelon
6MayOvocado
7JackBanana
8JackPapaya
Sheet1
 
Upvote 0
Solution
Hi Chornat,

I can't see how to put all in the same cell but I can repeat in the same row but multiple columns:

Chornat.xlsx
ABCDEFGHI
1NamePreferenceNamePreferences2nd3rd4th5th
2NatAppleNatAppleOrange   
3NatOrangeMayMelonOvocado   
4MayMelonJackBananaPapaya   
5MayOvocado      
6JackBanana      
7JackPapaya      
8      
9      
Sheet1
Cell Formulas
RangeFormula
D2:D9D2=INDEX($A$2:$A$999,MATCH(0,INDEX(COUNTIF($D$1:$D1,$A$2:$A$999),),0))&""
E2:I9E2=IF($D2="","",IFERROR(INDEX($B$2:$B$999,AGGREGATE(15,6,ROW($B$2:$B$999)-ROW($B$1)/($A$2:$A$999=$D2),COLUMN()-COLUMN($D$1))),""))

This also work as well. because then I can use CONCATENATE to combine F&G&H&I colume!
 
Upvote 0
Well let's get the ball rolling. Now this program will work. I know this should start the conversation going. Maybe some of the A students will weigh in and give us some more solutions.

VBA Code:
Sub Consol()

    Dim Name As String
    Dim Pref As String
    Dim LastRow As Integer
    Dim Row1 As Integer
   
    Row1 = 2
    LastRow = Range("A1", Range("A1").End(4)).Rows.Count
   
    For i = 2 To LastRow Step 2
   
    Cells(Row1, 4) = Cells(i, 1).Value
    Cells(Row1, 5) = Cells(Row1, 2) & vbNewLine & Cells(Row1 + 1 + 1, 2)
    Row1 = Row1 + 1
   
    Next i
   
End Sub

Book1
ABCDE
1NamePreference
2NatAppleNatApple Melon
3NatOrangeMayOrange Ovocado
4MayMelonJackMelon Banana
5MayOvocado
6JackBanana
7JackPapaya
Sheet4
Thank you for getting the ball rolling ^-^
 
Upvote 0
In future, please do not start a new thread in that circumstance. Refer to #12 of the Forum Rules and points 6 & 7 of the Guidelines.

For reference the other thread, which I have now locked, is here.

From that other thread it appears that items for one person may be repeated in the second column but you do not want them repeated in the results. If that is so, then you might try this macro.

VBA Code:
Sub CombinePrefs()
  Dim d As Object
  Dim a As Variant, b 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)
    If Not d.exists(a(i, 1)) Then d.Add a(i, 1), CreateObject("Scripting.Dictionary")
    d(a(i, 1))(a(i, 2)) = Empty
  Next i
  ReDim b(1 To d.Count, 1 To 2)
  For i = 1 To d.Count
    b(i, 1) = d.keys()(i - 1)
    b(i, 2) = Join(d.items()(i - 1).keys(), vbLf)
  Next i
  With Range("D2:E2").Resize(d.Count)
    .Value = b
    .Columns(2).WrapText = True
    .Rows(0).Value = Array("Name", "Preferences")
  End With
End Sub

My sample data and results:

chornat.xlsm
ABCDE
1NamePreferenceNamePreferences
2NatAppleNatApple Orange
3NatOrangeMayMelon Ovocado
4NatAppleJackBanana Papaya
5MayMelon
6MayOvocado
7JackBanana
8JackPapaya
Sheet1
First, Apologize for not carefully reading guideline.

Second, Thank you for the solution. I think this one is covered all steps. let me try and report back ^-^
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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