Concatenating a string in a "Many-to-One" relationship

pivotpp

New Member
Joined
Jun 23, 2017
Messages
12
Hi all,

Uploaded a pic of what I would like help in accomplishing: http://imgur.com/9j5n7LZ

Column a have teacher name, column b have student name. Each student can have more than 1 teacher.

For example, adam have all 3 teacher so I list their name Mr Robinson, ms Martinez, mr orwell (separated by a comma). Dan only have 1 teacher so it o Ky list Mr Robinson.

Any thoughts on a formula to accomllish this?

Thanks kindly in advance
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You might be able to do this with formulas if you have the newest version of Excel, but I am using XL2010 and you cannot do what you want with formulas in it. Here is a UDF (user defined function) that creates a function that can be used in a formula.
Code:
[table="width: 500"]
[tr]
	[td]Function Teachers(StudentName As String) As String
  Dim R As Long, Data As Variant
  Data = Range("A1", Cells(Rows.Count, "B").End(xlUp))
  For R = 1 To UBound(Data)
    If Data(R, 2) = StudentName Then Teachers = Teachers & ", " & Data(R, 1)
  Next
  Teachers = Mid(Teachers, 3)
End Function[/td]
[/tr]
[/table]

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use Teachers just like it was a built-in Excel function. For example,

=Teachers(B1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
If you have Excel 2010 or Office 365 with the TEXTJOIN function then:
This is an array formula that must be entered with CTRL-SHIFT-ENTER.
Excel Workbook
ABC
1Mr RobAdamMr Rob, Ms. Mar, Mr Or
2Mr RobBobMr Rob, Ms. Mar, Mr Or
3Mr RobCindyMr Rob, Mr Or
4Mr RobDanMr Rob
5Mr RobEdwardMr Rob
6Ms. MarWendyMs. Mar
7Ms. MarAdamMr Rob, Ms. Mar, Mr Or
8Ms. MarBobMr Rob, Ms. Mar, Mr Or
9Mr OrAdamMr Rob, Ms. Mar, Mr Or
10Mr OrBobMr Rob, Ms. Mar, Mr Or
11Mr OrCindyMr Rob, Mr Or
Sheet
 
Upvote 0
Thanks Rick, fat fingers on my part. That should have been Excel 2016 as you stated.
 
Upvote 0
Hi,

I believe even with Excel 2016, you Still need an Office 365 subscription to get the TEXTJOIN function...I have 2016 Without 365 and Don't have this function.

If you DO have TEXTJOIN, then AhoyNC has already given you what you need.

If you Don't, and is happy with a UDF, then Rick has already given you that also.

But if you Still want a formula solution, my suggestion requires a Blank or Header row on Top of your data, and a Helper Column (both of which you can "hide" out of sight):


Book1
ABCDE
1teacherstudentteachersteachers - final results<-- Helper row
2mr Robinsonadammr Robinsonmr Robinson, ms Martinez, mr Orwell
3mr Robinsonbobmr Robinsonmr Robinson, ms Martinez, mr Orwell
4mr Robinsoncindymr Robinsonmr Robinson, mr Orwell
5mr Robinsondanmr Robinsonmr Robinson
6mr Robinsonedwardmr Robinsonmr Robinson
7ms Martinezyasminems Martinezms Martinez, mr Orwell
8ms Martinezwendyms Martinezms Martinez
9ms Martinezadammr Robinson, ms Martinezmr Robinson, ms Martinez, mr Orwell
10ms Martinezbobmr Robinson, ms Martinezmr Robinson, ms Martinez, mr Orwell
11mr Orwellyasminems Martinez, mr Orwellms Martinez, mr Orwell
12mr Orwelladammr Robinson, ms Martinez, mr Orwellmr Robinson, ms Martinez, mr Orwell
13mr Orwellbobmr Robinson, ms Martinez, mr Orwellmr Robinson, ms Martinez, mr Orwell
14mr Orwellcindymr Robinson, mr Orwellmr Robinson, mr Orwell
15
16^^ Helper Column
Sheet390
Cell Formulas
RangeFormula
C2=IF(COUNTIF(B$2:B2,B2)=1,A2,LOOKUP(2,1/SEARCH(B2,B$1:B1),C$1:C1)&", "&A2)
D2=LOOKUP(2,1/SEARCH(B2,B$2:B$14),C$2:C$14)


Formulas copied down.
 
Upvote 0
Hi again,

Slightly shorter formula for C2 copied down:


Book1
ABCDE
1teacherstudentteachersteachers - final results<-- Helper row
2mr Robinsonadammr Robinsonmr Robinson, ms Martinez, mr Orwell
3mr Robinsonbobmr Robinsonmr Robinson, ms Martinez, mr Orwell
4mr Robinsoncindymr Robinsonmr Robinson, mr Orwell
5mr Robinsondanmr Robinsonmr Robinson
6mr Robinsonedwardmr Robinsonmr Robinson
7ms Martinezyasminems Martinezms Martinez, mr Orwell
8ms Martinezwendyms Martinezms Martinez
9ms Martinezadammr Robinson, ms Martinezmr Robinson, ms Martinez, mr Orwell
10ms Martinezbobmr Robinson, ms Martinezmr Robinson, ms Martinez, mr Orwell
11mr Orwellyasminems Martinez, mr Orwellms Martinez, mr Orwell
12mr Orwelladammr Robinson, ms Martinez, mr Orwellmr Robinson, ms Martinez, mr Orwell
13mr Orwellbobmr Robinson, ms Martinez, mr Orwellmr Robinson, ms Martinez, mr Orwell
14mr Orwellcindymr Robinson, mr Orwellmr Robinson, mr Orwell
15
16^^ Helper Column
Sheet390
Cell Formulas
RangeFormula
C2=IFERROR(LOOKUP(2,1/SEARCH(B2,B$1:B1),C$1:C1)&", ","")&A2
D2=LOOKUP(2,1/SEARCH(B2,B$2:B$14),C$2:C$14)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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