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:
  1. Module Module1
  2.     Public Function MyWordCount(ByVal TextToBeCounted As String) As Integer
  3.         Dim SpacePos As Integer ' Stores the value returned from Instring where a space char is found.
  4.         Dim X As Integer ' X tells InString from which char position to start from.
  5.         Dim WordCount As Integer ' How many words there are.
  6.         Dim NoMore As Boolean ' Yes or No.
  7.  
  8.         WordCount = 0
  9.         X = 1
  10.         NoMore = False
  11.  
  12.         If Len(Trim(TextToBeCounted)) > 0 Then
  13.             Do While NoMore = False
  14.                 SpacePos = InStr(X, Trim(TextToBeCounted), " ")
  15.                 If SpacePos > 0 Then
  16.                     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
  17.                         WordCount += 1
  18.                     End If
  19.                     X = SpacePos + 1
  20.                     Do While InStr(X, Mid(TextToBeCounted, X, 1), " ") > 0
  21.                         X += 1
  22.                     Loop
  23.                 Else
  24.                     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
  25.                         WordCount += 1
  26.                     End If
  27.                     NoMore = True
  28.                 End If
  29.  
  30.             Loop
  31.  
  32.         End If
  33.         MyWordCount = WordCount
  34.     End Function
  35. End Module