Counts the number of words in a string.
Counts the number of words in a string.
Rate Counts the number of words in a string.
(1(1 Vote))
'this function finds the number of spaces in a string and from that
'determines how many words there are.
Dim SpaceAlreadyFound As Boolean, NumOfSpaces As Long, n As Long
Dim Character As String
'return zero if the string is empty.The trim ensures
'that a string of spaces is counted as invalid
If Trim(StringForWordCount) = "" Then Exit Function
'remove leading and trailing spaces
StringForWordCount = Trim(StringForWordCount)
'count the words
For n = 1 To Len(StringForWordCount) - 1
'go through each character
Character = Mid(StringForWordCount, n, 1)
'check if selected character is a space
If Character = Chr(32) Then
'check if a space has been found already
If SpaceAlreadyFound Then
'dont increment space count
Else
NumOfSpaces = NumOfSpaces + 1
SpaceAlreadyFound = True
End If
Else
'reset this because another character
'has been found
SpaceAlreadyFound = False
End If
Next n
'always add 1, since the number of words
'in a string is equivalent to the number
'of spaces + 1
WordCount = NumOfSpaces + 1
End Function
Counts the number of words in a string. Comments
No comments yet — be the first to post one!
Post a Comment