View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
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