How to count words between first and last comma?

Okoth

Board Regular
Joined
Sep 10, 2009
Messages
106
Office Version
  1. 2019
Platform
  1. Windows
I'm looking for a way to count the words between the first and last comma. The sentence in the cell is dynamic. An example is this

Get a pan ready. Add milk, eggs, pepper, salt, half the sugar, onions and ham.

I can imagine the easiest way of doing it is to count the total number of words in the sentence(s) and deduct the number of words before the first comma and words after the last comma from it, but I don't know how to do it.

Please help

Thanks
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
For your example, what result do you expect? Enter the result.
 
Upvote 0
In the case of this sentence: "Get a pan ready. Add milk, eggs, pepper, salt, half the sugar, onions and ham." the result should be 6, because that is the number of words between the first and the last comma.
 
Upvote 0
In the case of this sentence: "Get a pan ready. Add milk, eggs, pepper, salt, half the sugar, onions and ham." the result should be 6, because that is the number of words between the first and the last comma.

If your sentence is in cell A2 try the following. The formula is little bit long, but does what is required by you.

Book3
B
26
Sheet1
 
Upvote 0
Number of words (each divided by a space), between 1st and last comma:

=LEN(TRIM(MID(LEFT(A2,LOOKUP(1E+100,FIND(",",A2,ROW(INDIRECT("1:"&LEN(A2)))))-1),SEARCH(",",A2)+1,LEN(A2))))-LEN(SUBSTITUTE(TRIM(MID(LEFT(A2,LOOKUP(1E+100,FIND(",",A2,ROW(INDIRECT("1:"&LEN(A2)))))-1),SEARCH(",",A2)+1,LEN(A2)))," ",""))+1

We can avoid using the volatile INDIRECT function by using ROW(1:255) instead also presuming the length of cell A2 string would be within this limit.
 
Upvote 0
Amazing how you guys do these things. No wonder I couldn't figure this out. It was even more complicated than I thought.

Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,981
Members
449,276
Latest member
surendra75

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