Counting number of words in a sentence

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
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
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,850
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
Office Version
  1. 2016
Platform
  1. Windows
Re: Counting number os words in a sentence

Thanks Mark, can I have vba script for it?

Want to learn more
Thanks again
Kelly
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,850
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,879
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

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
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,850
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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:

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,513
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.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,850
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,737
Messages
5,597,826
Members
414,180
Latest member
Sir Khaya

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
Top