Hey guys and gals, I have spent (with the help of a friend) 2 days working on this piece of code for a word counter.
Now we have tested and tested all sorts of combinations, and so far I have not come along a single piece that does not work.
But if you have any suggestions on how to improve please let me know!
Module Module1 Code:
Module Module1 Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found. Dim X As Integer ' X tells InString from which char position to start from. Dim WordCount As Integer ' How many words there are. Dim NoMore As Boolean ' Yes or No. WordCount = 0 X = 1 NoMore = False If Len(Trim(TextToBeCounted)) > 0 Then Do While NoMore = False SpacePos = InStr(X, Trim(TextToBeCounted), " ") If SpacePos > 0 Then If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then WordCount += 1 End If X = SpacePos + 1 Do While InStr(X, Mid(TextToBeCounted, X, 1), " ") > 0 X += 1 Loop Else If Asc(Mid(TextToBeCounted, X, 1)) > 64 And Asc(Mid(TextToBeCounted, X, 1)) < 91 Or Asc(Mid(TextToBeCounted, X, 1)) > 96 And Asc(Mid(TextToBeCounted, X, 1)) < 123 Or Asc(Mid(TextToBeCounted, X, 1)) > 47 And Asc(Mid(TextToBeCounted, X, 1)) < 58 Then WordCount += 1 End If NoMore = True End If Loop End If MyWordCount = WordCount End Function End Module





Reply With Quote