Counting number of words in a sentence

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
1. I am looking for a script to count the number of words in a sentence from a word file.

2. Also to count number of words in a given cell in Excel

Thanks for your attention
Kelly
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Re: Counting number os words in a sentence

2. Also to count number of words in a given cell in Excel
If there are only single spaces between the words...

Excel Workbook
AB
1Also to count number of words in a given cell in Excel12
2AND1
3AND TWO2
4one two three four five5
Sheet3
 
Upvote 0
Re: Counting number os words in a sentence

Thanks Mark, can I have vba script for it?

Want to learn more
Thanks again
Kelly
 
Upvote 0
Re: Counting number os words in a sentence

Code:
Sub cWords()
    With Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row)
        .FormulaR1C1 = _
        "=IF(LEN(TRIM(RC[-1]))-LEN(SUBSTITUTE(TRIM(RC[-1]),"" "",""""))>0,LEN(TRIM(RC[-1]))-LEN(SUBSTITUTE(TRIM(RC[-1]),"" "",""""))+1,1)"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Re: Counting number os words in a sentence

Code:
Sub cWords()
    With Range("B1:B" & Range("A" & Rows.Count).End(xlUp).Row)
        .FormulaR1C1 = _
        "=IF(LEN(TRIM(RC[-1]))-LEN(SUBSTITUTE(TRIM(RC[-1]),"" "",""""))>0,LEN(TRIM(RC[-1]))-LEN(SUBSTITUTE(TRIM(RC[-1]),"" "",""""))+1,1)"
        .Value = .Value
    End With
End Sub

Okay thanks
But I need explanation small
Thanks again
Kelly
 
Upvote 0
Re: Counting number os words in a sentence

Range is from B1 to B and last row in column A.
Not counting the Sub line....
2nd row puts in the same formula as post 2.
Row 3 changes it from formula to values.
 
Last edited:
Upvote 0
1. I am looking for a script to count the number of words in a sentence from a word file.
VBA has no idea what a grammatical sentence is. For example, consider the following:
Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
For you and me, that would count as one sentence; with VBA in Word it counts as 5 sentences.
 
Upvote 0
Seeing as it has been moved to the General forum because of the Word part of the question (and that board doesn't support HTML) the formula in post number 2 is
=IF(LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))>0,LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+1,1)

to save anyone trying to decipher it.
 
Upvote 0

Forum statistics

Threads
1,214,521
Messages
6,120,018
Members
448,937
Latest member
BeerMan23

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