Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3
Default How to tally words in Excel?


  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default How to tally words in Excel?

On Sat, 11 Aug 2007 06:46:00 -0700, Looking for answers <Looking for
wrote:



Count the spaces; add one (1)

=1+LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))
--ron
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 396
Default How to tally words in Excel?

Hi

Here's a VBA approach:

Function CountWordsCell(s As String)

Dim a As Variant

a = Split(Trim(s))

CountWordsCell = UBound(a) + 1

End Function

Paste this function into a new module.

You can then use this function anywhere in your sheet.

You use it like:

=CountWordsCell(C7)


--
Wigi
http://www.wimgielis.be = Excel/VBA, soccer and music


"Ron Rosenfeld" wrote:

On Sat, 11 Aug 2007 06:46:00 -0700, Looking for answers <Looking for
wrote:



Count the spaces; add one (1)

=1+LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))
--ron

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default How to tally words in Excel?

But to avoid returning 1 when the cell is empty:

=LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+(TRIM(A1)<"")

Ron Rosenfeld wrote:

On Sat, 11 Aug 2007 06:46:00 -0700, Looking for answers <Looking for
wrote:

Count the spaces; add one (1)

=1+LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))
--ron


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default How to tally words in Excel?

On Sat, 11 Aug 2007 19:15:49 -0500, Dave Peterson
wrote:

But to avoid returning 1 when the cell is empty:

=LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+(TRIM(A1)<"")


Good point
--ron


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,202
Default How to tally words in Excel?

See the inline comment....

Here's a VBA approach:

Function CountWordsCell(s As String)

Dim a As Variant

a = Split(Trim(s))


VBA's Trim function does not work like the spreadsheet formula's TRIM
function... it only removes external blanks.. multiple internal blanks will
remain. So, the above line will not reduce consecutive blank spaces to a
single blank; hence, your count would be incorrect in that situation.
Replace the above line with this...

a = s
Do While InStr(a, " ")
a = Replace(a, " ", " ")
Loop

Rick




CountWordsCell = UBound(a) + 1

End Function

Paste this function into a new module.

You can then use this function anywhere in your sheet.

You use it like:

=CountWordsCell(C7)


--
Wigi
http://www.wimgielis.be = Excel/VBA, soccer and music


"Ron Rosenfeld" wrote:

On Sat, 11 Aug 2007 06:46:00 -0700, Looking for answers <Looking for
wrote:



Count the spaces; add one (1)

=1+LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))
--ron


  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default How to tally words in Excel?

On Sun, 12 Aug 2007 04:19:08 -0400, "Rick Rothstein \(MVP - VB\)"
wrote:

VBA's Trim function does not work like the spreadsheet formula's TRIM
function... it only removes external blanks.. multiple internal blanks will
remain. So, the above line will not reduce consecutive blank spaces to a
single blank; hence, your count would be incorrect in that situation.
Replace the above line with this...

a = s
Do While InStr(a, " ")
a = Replace(a, " ", " ")
Loop

Rick


Rick,

Instead of the loop, one could also use:

a = application.worksheetfunction.trim(a)


--ron
  #8   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,202
Default How to tally words in Excel?

VBA's Trim function does not work like the spreadsheet formula's TRIM
function... it only removes external blanks.. multiple internal blanks
will
remain. So, the above line will not reduce consecutive blank spaces to a
single blank; hence, your count would be incorrect in that situation.
Replace the above line with this...

a = s
Do While InStr(a, " ")
a = Replace(a, " ", " ")
Loop

Rick


Rick,

Instead of the loop, one could also use:

a = application.worksheetfunction.trim(a)


That's true... having been (and still am) a compiled VB person for more than
10 years now, I tend to think of solutions in terms of pure language code,
hence the loop. I have often wondered, though, is there a time (or
efficiency) penalty of any kind to pay for using
Application.WorkSheetFunction? I'm thinking since there is a time penalty to
pay when the spreadsheet world reaches into the VBA world for a solution,
that there is probably one to when going in the reciprocal direction too. Do
you know if that is the case?

Rick

  #9   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default How to tally words in Excel?

On Sun, 12 Aug 2007 10:27:44 -0400, "Rick Rothstein \(MVP - VB\)"
wrote:

That's true... having been (and still am) a compiled VB person for more than
10 years now, I tend to think of solutions in terms of pure language code,
hence the loop. I have often wondered, though, is there a time (or
efficiency) penalty of any kind to pay for using
Application.WorkSheetFunction? I'm thinking since there is a time penalty to
pay when the spreadsheet world reaches into the VBA world for a solution,
that there is probably one to when going in the reciprocal direction too. Do
you know if that is the case?


I don't know.
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula in excel to run a tally Diana Excel Worksheet Functions 1 January 26th 07 12:36 AM
create tally sheet for positions- insert names and tally # tally sheets Excel Discussion (Misc queries) 0 April 11th 06 09:41 PM
how do i make a tally chart in excel? steph_wales Charts and Charting in Excel 0 October 4th 05 03:51 PM
Tally function in excel bgbrady Excel Worksheet Functions 1 August 10th 05 08:46 PM
How do I put tally counts in Excel? KK Excel Discussion (Misc queries) 3 February 26th 05 11:52 PM


All times are GMT +1. The time now is 04:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"