Textjoin function to create a comma separated list with "and"

excel151515

New Member
Joined
Apr 17, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Long time reader, first time poster. This is the first time I have not been able to find a solution to my problem.

My goal is to get excel to join a series of texts into a sentence that has commas between each text string, with "and" between the last and second last string. I can create the comma separated list using the textjoin function, but cannot figure out how to insert an "and" prior to the last string.

By way of example, I have 3-6 points of criteria, for example colours, that may apply to a specific product. For "Product A", where "blue" "red" and orange" apply, I want a cell which states "blue, red, and orange".

Any help is greatly appreciated!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the MrExcel board. This is messy, but here is one way. There should be a convenient option for the Oxford comma:
mrexcel_20200416.xlsm
JKLMN
1
2blueredorangeblue, red, and orange
Sheet23
Cell Formulas
RangeFormula
N2N2=SUBSTITUTE(TEXTJOIN(", ",TRUE,J2,K2,L2),",",", and",LEN(TEXTJOIN(", ",TRUE,J2,K2,L2))-LEN(SUBSTITUTE(TEXTJOIN(", ",TRUE,J2,K2,L2),",","")))
 
Upvote 0
Welcome to the MrExcel board. This is messy, but here is one way. There should be a convenient option for the Oxford comma:
mrexcel_20200416.xlsm
JKLMN
1
2blueredorangeblue, red, and orange
Sheet23
Cell Formulas
RangeFormula
N2N2=SUBSTITUTE(TEXTJOIN(", ",TRUE,J2,K2,L2),",",", and",LEN(TEXTJOIN(", ",TRUE,J2,K2,L2))-LEN(SUBSTITUTE(TEXTJOIN(", ",TRUE,J2,K2,L2),",","")))

KRice this is fantastic thank you. I have 2 follow up questions.
1. How would I go about adding categories, I actually have 7 in total.
2. How can this be modified so that if only 1 category applies, it simply states the one category ie. "blue.". Currently, if only 1 applies it gives a value of #VALUE!

I can't tell you how many hours I've spent trying to solve this so I very much appreciate it.
 
Upvote 0
Additional categories are easily added by adding onto the concatenation string, but that would have to be done in three places in that single formula. I see two issues...you mentioned one...the other involves just two categories, where you probably do not want the comma. For example, should it read as "blue and yellow"?

I'll post an idea for resolving this.
 
Upvote 0
Unfortunately, these exceptions require expanding the formula so that there are now 5 sets of TEXTJOIN functions that need to be updated. If you are not opposed to a helper cell (which could be hidden), the TEXTJOIN could be moved to a different column, and then this function would simply incorporate a reference to the cell containing the TEXTJOIN formula...and then there would be only one TEXTJOIN function to update with additonal categories...see the lower block in the sheet as an example.
mrexcel_20200416.xlsm
IJKLMNOPQ
1
2blueredorangeyellowpurpleblue, red, orange, yellow, and purple
3blueorangeyellowpurpleblue, orange, yellow, and purple
4blueyellowblue and yellow
5blueblue
6all blanks -> 
7Resultshelper column (hide)
8blueredorangeyellowpurpleblue, red, orange, yellow, and purpleblue, red, orange, yellow, purple
9blueorangeyellowpurpleblue, orange, yellow, and purpleblue, orange, yellow, purple
10blueyellowblue and yellowblue and yellow
11redredred
12all blanks ->  
Sheet23
Cell Formulas
RangeFormula
P2:P6P2=IF(COUNTA(J2:N2)=2,TEXTJOIN(" and ",TRUE,J2,K2,L2,M2,N2),IFERROR(SUBSTITUTE(TEXTJOIN(", ",TRUE,J2,K2,L2,M2,N2),",",", and",LEN(TEXTJOIN(", ",TRUE,J2,K2,L2,M2,N2))-LEN(SUBSTITUTE(TEXTJOIN(", ",TRUE,J2,K2,L2,M2,N2),",",""))),TEXTJOIN(", ",TRUE,J2,K2,L2,M2,N2)))
P8:P12P8=IFERROR(SUBSTITUTE(Q8,",",", and",LEN(Q8)-LEN(SUBSTITUTE(Q8,",",""))),Q8)
Q8:Q12Q8=TEXTJOIN(IF(COUNTA(J8:N8)=2," and ",", "),TRUE,J8,K8,L8,M8,N8)
 
Upvote 0
How about:

Book1
IJKLMNOP
2blueredorangeyellowpurpleblue, red, orange, yellow and purple
3blueorangeyellowpurpleblue, orange, yellow and purple
4blueyellowblue and yellow
5blueblue
6all blanks -> 
Sheet1
Cell Formulas
RangeFormula
P2:P6P2=SUBSTITUTE(TEXTJOIN(", ",TRUE,J2:N2),","," and",MAX(1,COUNTA(J2:N2)-1))


It lacks the Oxford comma, but makes up for it with a fairly simple formula. I guess it depends on your style book.

Edit: This has the Oxford comma:

=SUBSTITUTE(TEXTJOIN(", ",TRUE,J2:N2),",",IF(COUNTA(J2:N2)>2,", and"," and"),MAX(1,COUNTA(J2:N2)-1))
 
Last edited:
Upvote 0
I like Eric's consolidation within the TEXTJOIN function. If all of the cells to join are contiguous, then this offers the best of both, I think...shorter formula and the Oxford comma...but it does use a helper cell.
mrexcel_20200416.xlsm
IJKLMNOPQ
7Resultshelper column (hide)
8blueredorangeyellowpurpleblue, red, orange, yellow, and purpleblue, red, orange, yellow, purple
excel151515
Cell Formulas
RangeFormula
P8P8=IFERROR(SUBSTITUTE(Q8,",",", and",LEN(Q8)-LEN(SUBSTITUTE(Q8,",",""))),Q8)
Q8Q8=TEXTJOIN(IF(COUNTA(J8:N8)=2," and ",", "),TRUE,J8:N8)
 
Upvote 0
If you look back at my post #6, I managed to edit it in time to add a version with the Oxford comma.
 
Upvote 0
I used another feature in Eric's solution to further shorten the formula, and now it is practical to eliminate the helper cell and incorporate just one TEXTJOIN function in the formula. The range J23:N23 (3 places) needs to be adjusted to accommodate more categories.
mrexcel_20200416.xlsm
IJKLMNOP
22Results
23blueredorangeyellowpurpleblue, red, orange, yellow, and purple
24blueorangeyellowpurpleblue, orange, yellow, and purple
25blueyellowblue and yellow
26redred
27all blanks -> 
excel151515
Cell Formulas
RangeFormula
P23:P27P23=SUBSTITUTE(TEXTJOIN(IF(COUNTA(J23:N23)=2," and ",", "),TRUE,J23:N23),",",", and",MAX(1,COUNTA(J23:N23)-1))
 
Upvote 0
Perfect!...no, sorry, I missed that. Nice work!
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

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