Generate list of text values and include "and" when it's the last value.

amautaexcel

New Member
Joined
Jun 30, 2017
Messages
7
Dear Excel Forum,

I am stumped by the following problem. I hope one of you can help me.
Goal: Create a sentence that lists items and it inserts the word "and" for the last entry.

I have a table where the user inputs the prices. If the price is included the items and the price is included in a sentence in a different sheet.

Apples . $5
Oranges $1
Pears $3
Bananas . $2

So for the table above, it would be: "Buy Apples $5, Oranges $1, Pears $3 and Bananas $2"

If the table only had apples and pears, it would be "Buy Apples $5 and Pears $3"
If the table only had pears and bananas it would be "Buy Pears $3 and Bananas $2"

I can string the text with if statements, but I have not been able to figure out how to tell it to include the "and" for the last entry.

Any thoughts?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Do a count on the spaces, then substitute the last space for spaceANDspace

<style><!--table {mso-displayed-decimal-separator:"\."; mso-displayed-thousand-separator:"\,";}@page {margin:.75in .7in .75in .7in; mso-header-margin:.3in; mso-footer-margin:.3in;}td {padding-top:1px; padding-right:1px; padding-left:1px; mso-ignore:padding; color:black; font-size:12.0pt; font-weight:400; font-style:normal; text-decoration:none; font-family:Calibri, sans-serif; mso-font-charset:0; mso-number-format:General; text-align:general; vertical-align:bottom; border:none; mso-background-source:auto; mso-pattern:auto; mso-protection:locked visible; white-space:nowrap; mso-rotate:0;}--></style>[TABLE="width: 87"]
<!--StartFragment--> <colgroup><col width="87" style="width:65pt"> </colgroup><tbody>[TR]
[TD="width: 87"]Do you mind elaborating on this?[/TD]
[/TR]
[TR]
[TD]How can I replace just the last to " and "[/TD]
[/TR]
<!--EndFragment--></tbody>[/TABLE]
Vba?

Thank you very much.
 
Upvote 0
If you have Excel 2016, you could use the function ... TextJoin .... which is purpose-built to do exactly what you want.

However, if you don't have Excel 2016, I need to know a few things from you ...

* Is a user ever going to input more than 7 items .... remember, you can only have a maximum of 7 if statements in a formula
* Is the item and its corresponding price in the same cell, or 2 separate cells

Kind regards,

Chris
 
Upvote 0
My apologies. It's supposed to be a table, 2 columns.

Fruit Cost
Apples $5
Oranges $1
Pears $3
Bananas $2

The table has no prices and the user gets to put the prices in. Depending if the price is filled out a sentence would string the text together, so for the completed table it would be like this:

So for the table above, it would be: "Buy Apples $5, Oranges $1, Pears $3 and Bananas $2"

If the table only had apples, pears and bananas, it would be "Buy Apples $5, Pears $3 and Bananas $2"
If the table only had pears and bananas it would be "Buy Pears $3 and Bananas $2"

The problem that I am having is that I cannot tell it to put an "and" right before the last entry. Similarly, if there's only one entry, i could not say "Buy and Apples $5".

I wrestled with this all weekend and I am stuck.

Thank you for your help.
 
Upvote 0
I have excel 2016 and we are working with less that 7 items.The data is in a table of 2 columns.

If you have TEXTJOIN on your 2016 system...

Control+shift+enter, not just enter:

=IF(COUNT(B2:B5),"Buy ","")&TEXTJOIN(", ",TRUE,IF(ISNUMBER(B2:B5),A2:A5&" "&B2:B5,""))
 
Upvote 0
If you have TEXTJOIN on your 2016 system...

Control+shift+enter, not just enter:

=IF(COUNT(B2:B5),"Buy ","")&TEXTJOIN(", ",TRUE,IF(ISNUMBER(B2:B5),A2:A5&" "&B2:B5,""))

TextJoin does not account for the "and" before the last entry. Only seems to put commas and spaces. Am I missing something?

Again, the example would be, if they have Bananas and apples only, it should read "Buy Apples $$ and Bananas $$"

Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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