Count words between two characters

Okoth

Board Regular
Joined
Sep 10, 2009
Messages
106
Office Version
  1. 2019
Platform
  1. Windows
What I'm trying to achieve is that if there is a text in A1 like this

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ut lectus est. Pellentesque cursus ut ipsum sed commodo. Mauris ullamcorper, odio ultricies scelerisque dictum, quam mauris dapibus justo, sit amet ornare est urna at risus. Quisque sodales euismod nunc a vulputate.

I want to put in B1 a message that says "Sentence too long" if one or more of the sentences in A1 has more than 15 words.

Is it possible to count the number of words between multiple periods in a cell?
 
@PeterSS
I see the Like function has disappeared...which is probably a good thing....but I really did like your snippet of code. A very nice solution indeed !!!...:pray:
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I'm getting a #NAME? error: Ambiguous name detected.
That would indicate that you have two (or more) vba functions in the workbook with the name 'CheckSentences'


I see a slight difference with your explanation.
"3. Copy and Paste the code below into the main right hand pane". I see only one screen.
In the vba window (where the code is) in the 'View' menu at the top, click on 'Project Explorer'. That should open another pane in the vba window (usually at the left) where you can see the various projects (workbooks) that you have open and their various Worksheets, Modules etc. Then the one with the actual code is usually to the right of that Project Explorer window pane.


@PeterSS
I see the Like function has disappeared...which is probably a good thing....but I really did like your snippet of code. A very nice solution indeed !!!...:pray:
Thank you Michael. :)
 
Last edited:
Upvote 0
@PeterSS
I see the Like function has disappeared...which is probably a good thing
Why? I thought it was a great way to allow readers of the thread to show their appreciation for a post without clogging up the forum's servers with lots of "Way too go!" type messages.



Rich (BB code):
Function CheckSentences(s As String) As String
  Dim aSentences As Variant, aWords As Variant
  Dim msg As String
  Dim i As Long
  
  Const MaxLength As Long = 15 '<- Maximum words allowed in a sentence
  
  aSentences = Split(Replace(Application.Trim(Replace(Replace(s, "?", "."), "!", ".")), ". ", "."), ".")
  For i = 0 To UBound(aSentences)
    aWords = Split(aSentences(i))
    If UBound(aWords) >= MaxLength Then
      msg = msg & ", " & i + 1 & "(" & UBound(aWords) + 1 & ")"
    End If
  Next i
  If Len(msg) = 0 Then
    CheckSentences = "All Ok"
  Else
    CheckSentences = "These sentences are too long: " & Mid(msg, 3)
  End If
End Function
One caveat with your UDF (user defined function)... it will be fooled by abbreviations such as Dr., Mr., Ph.d, etc. If the OP is okay with the function not identifying which sentence or sentences are too long, then this UDF could be used...
Code:
[table="width: 500"]
[tr]
	[td]Function CheckSentences(s As String) As String
  CheckSentences = Choose(2 + ("." & s & "." Like "[.?!]" & Application.Rept("*?* ", 15) & "*?*[.?!]"), "Too long", "All OK")
End Function
[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
One caveat with your UDF (user defined function)... it will be fooled by abbreviations such as Dr., Mr., Ph.d, etc.
True, but I'm not sure how likely that is. Did you see the sample data?

BTW, yours is fooled by this. ;)
"The cat sat on the mat. The dog ran away. The mouse squeaked. The horse came to look."
 
Upvote 0
BTW, yours is fooled by this. ;)
"The cat sat on the mat. The dog ran away. The mouse squeaked. The horse came to look."
Interesting... I was just thinking about the function being fooled by those asterisks... I'll have to think about this some more.
 
Upvote 0
I agree with the comment

I thought it was a great way to allow readers of the thread to show their appreciation for a post without clogging up the forum's servers with lots of "Way too go!" type messages.
But sadly, just like where it probably originated (facebook), it gets overused and becomes a nuisance rather than a thank you !!
I believe a good old fashioned please and thank you show much more appreciation than ticking a like button....
 
Upvote 0
I'll have to think about this some more.
I doubt that it is possible to deal completely with abbreviations, even if a comprehensive list of them was provided. Consider these two examples:

Mike watched while Dr. Smith treated his wife. (1 sentence)
The abbreviation for doctor is Dr. Smith is a noun used to describe a metal worker. (2 sentences)
 
Upvote 0
This is great and it is going to be helpful in so many of my sheets. Thank you very much!
 
Upvote 0
I doubt that it is possible to deal completely with abbreviations, even if a comprehensive list of them was provided. Consider these two examples:

Mike watched while Dr. Smith treated his wife. (1 sentence)
The abbreviation for doctor is Dr. Smith is a noun used to describe a metal worker. (2 sentences)
What a great example! Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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