Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Word Count 2

Status
Not open for further replies.

Basia

Programmer
Apr 27, 2001
64
ML
Greetings.

I need to count the number of words in a memo box. Can help me figure this one out?

Thanks in advance,
Basia
 
Hi Basia!

Try using this function:

Dim strTrimmed As String
Dim intCount As Integer
Dim lngSpace As Long
Dim lngNextSpace As Long

strTrimmed = Trim(YourTextBox.Value)
intCount = 1
lngSpace = InStr(strTrimmed, " ")
lngNextSpace = InStr(lngSpace, strTrimmed, " ")
Do Until lngNextSpace = 0
intCount = intCount + 1
lngSpace = lngNextSpace
lngNextSpace = InStr(lngSpace, strTrimmed, " ")
Loop

After the loop runs intCount will hold the number of words in YourTextBox assuming that each word is separated by a space and there are no other internal spaces in the text box. The trim function will take care of any external spaces. There are several good find and replace functions on the site the you could use to replace " " with " " if you want to. Of course there would then be the question are there any " " etc. If it is important to you can make this change in the loop:

Do Until lngNextSpace = 0
intCount = intCount + 1
lngSpace = lngNextSpace
lngNextSpace = InStr(lngSpace, strTrimmed, " ")
Do Until lngNextSpace - lngSpace <> 1
lngSpace = lngNextSpace
lngNextSpace = InStr(lngSpace, strTrimmed, &quot; &quot;)
Loop
Loop

hth
Jeff Bridgham
bridgham@purdue.edu
 
Depends (somewhat) on the details, but using split (Ms. A. 2K) or basSplit for ealier ver), create the array of words and just use the UBound (+1) for the word count.

Code:
Public Function basWdCount(strIn As String) As Long

    'Michael Red    4/23/02 Just return the # of &quot;words in the string

    Dim MyWds As Variant

    MyWds = basSplit(strIn, &quot; &quot;)
    basWdCount = UBound(MyWds) + 1

End Function

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Thanks for all the examples. I will try them out today and let you all know!

Thanks for the quick responses...Basia :)
 
Thanks to everyone - I got it to work perfectly. I used Michael Red's funciton.

Phew...Much appreciated!
Basia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top