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!
 
Gentlemen, again, this is awesome so thank you very much. The last version that gets rid of the help column does not seem to work. It's not working because the "blank" cells are blank because of an IF statement that results in "", as opposed to being truly blank.

Is there a way to remove the comma before and if there is only 2 results?

Thanks again!
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Are all of the "blanks" generated by IF statements that insert a null (""), or are only some blanks caused by this?
 
Upvote 0
Try this. The issue is the COUNTA function. I replaced it with SUMPRODUCT to determne how many cells were not occupied by a "".
mrexcel_20200416.xlsm
IJKLMNOP
23blueredorangeyellowpurpleblue, red, orange, yellow, and purple
24blueorangeyellowpurpleblue, orange, yellow, and purple
25blueyellowblue and yellow
26redred
27blanks due to IF ->herethere   here and there
excel151515
Cell Formulas
RangeFormula
J27:K27J27=IF(F$12=1,F11,"")
L27:N27L27=IF(H$28=1,H27,"")
P23:P27P23=SUBSTITUTE(TEXTJOIN(IF(SUMPRODUCT(--(J23:N23<>""))=2," and ",", "),TRUE,J23:N23),",",", and",MAX(1,COUNTA(J23:N23)-1))
 
Upvote 0
The COUNTA function that was left in the previous post can lead to some odd results, but I haven't encountered any examples where that becomes a problem. This version might be a little better...this replaces both COUNTA functions:

=SUBSTITUTE(TEXTJOIN(IF(SUMPRODUCT(--(J27:N27<>""))=2," and ",", "),TRUE,J27:N27),",",", and",MAX(1,SUMPRODUCT(--(J27:N27<>""))-1))
 
Upvote 0
Gentlemen, this has officially solved my issues. I can now move on to other parts of my workbook to find some automation to employ!

Thank you both again.
 
Upvote 0
Great news! You're welcome...we're happy to help. This problem reminded me about the different meanings of "nothing."
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,727
Members
449,116
Latest member
Aaagu

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