Count Words in a Field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #16
    Originally posted by Andrew Thackray
    Never knew there wuz a slpit function. However nothing new under the sun. Looking up split on the net resulted in this set of handy routines, including wordcount. http://j-walk.com/ss/excel/tips/tip93.htm
    Definitely bookmarked - thanks.

    Comment

    • PEB
      Recognized Expert Top Contributor
      • Aug 2006
      • 1418

      #17
      Very good exemple pks00!

      The function that i've use was:

      Function word_count(my_s entence, sep) As Integer
      Dim result As Integer
      Dim my_str
      result = 1
      Do While InStr(1, my_str, sep) > 0
      result = result + 1
      my_str = Mid(my_str, InStr(1, my_str, sep) + 1, Len(my_str))
      Loop
      word_count = result
      End Function

      But yours it's great!

      Best regards!


      Originally posted by pks00
      What if u used the Split command?


      Code:
      Dim sWords() as String
      
      sWords = Split(myfield," ")
      
      msgbox "Number of words is " & ubound(sWords)

      Comment

      • novel
        New Member
        • Nov 2006
        • 2

        #18
        Thank you all for your input! I appreciate the help. However - and I know this might be a stupid question - but how would I use this code in Access????

        Comment

        • PEB
          Recognized Expert Top Contributor
          • Aug 2006
          • 1418

          #19
          :)

          Ok my friend, we've done a big discussion about your problem...

          But you need to select a function it's better the suggestion of pks00 copy from thescripts and

          in access go in modules, choose New module and Paste there the function

          Save it...

          To obtain the word count in a column in your query:

          Create a query add your table or query on which should be based the new one.

          In an empty column write the name of the choosen function and between the () instaed the name inside, put the name of the column that contain the text that you want to get the count

          Comment

          • stevepsft
            New Member
            • Mar 2017
            • 2

            #20
            Here's a simple function

            Comment

            • stevepsft
              New Member
              • Mar 2017
              • 2

              #21
              Code:
              public function word_count(InWord as variant) as Integer
              dim words() as string
              
              word_str=nz(word_str,"")   'deals with null values passed in
              word_str = replace(word_str,vbcr," ")  ' Whatever you want to also delineate a word can be placed here.
              
              while instr(word_str,"  ") > 0 )  'Check to see if double spaces
                 word_str = replace(word_str,"  "," ") ' change double to single space
              wend
              words = split(word_str," ")
              word_count = ubound(words) + 1 
              end function

              Comment

              • ahmedtharwat19
                New Member
                • Feb 2007
                • 55

                #22
                pks00 Good statement

                Thank you all
                Ahmed

                Comment

                Working...