Function Countem(myString As String) As Integer
Dim CurrentChar As String
Dim LetterCount(32 To 90) As Integer
Dim TotalCharacters As Integer
Dim i As Integer, j As Integer
Dim NumCount As Integer
Dim WordCount As Integer
'Total length of string, including leading spaces
TotalCharacters = Len(myString)
Debug.Print "Total characters = " & TotalCharacters
'Number of words in string.
WordCount = StrCount(onespace(myString), " ") + 1
Debug.Print "Total words = " & WordCount
'for the letter count…
For i = 1 To TotalCharacters
CurrentChar = Mid(myString, i, 1)
j = Asc(CurrentChar)
If j >= 32 And j <= 90 Then
LetterCount(j) = LetterCount(j) + 1
End If
Next i
'number of valid (between 32 AND 90) characters
Debug.Print "Character Distribution:"
NumCount = 0
For j = 32 To 90
NumCount = NumCount + LetterCount(j)
If LetterCount(j) > 0 Then
Debug.Print "Asc(" & j & ")= " & Chr(j) & ": " & LetterCount(j); ""
End If
Next j
Debug.Print "Valid Characters: "
Countem = NumCount
End Function
'***************************************************************************
Function StrCount(ByRef TheStr As String, theItem As Variant) As Integer
'------------------------------------------------------------------
' PURPOSE: Counts the numbers of times an item occurs
' in a string.
' ARGUMENTS: TheStr: The string to be searched.
' TheItem: The item to search for.
' RETURNS: The number of occurences as an integer.
'
' NOTES: To test: Type '? StrCount("The quick brown fox jumped over
' the lazy dog", "the") in the debug window.
' The function will return 2.
'------------------------------------------------------------------
Dim strHold As String, itemhold As Variant
Dim placehold As Integer
Dim i As Integer, j As Integer
strHold = TheStr
itemhold = theItem
j = 0
If InStr(1, strHold, itemhold) > 0 Then
While InStr(1, strHold, itemhold) > 0
placehold = InStr(1, strHold, itemhold)
j = j + 1
strHold = Mid(strHold, placehold + Len(itemhold))
Wend
'Debug.Print "StrCount= " & j
End If
StrCount = j
End Function
'------------------------------------------------------------------
Function onespace(pstr As String)
'*******************************************
'Name: onespace (Function)
'Purpose: Removes excessive spaces from a string
'Inputs: call onespace(" the quick brown fox")
'Output: "the quick brown fox"
'*******************************************
Dim strHold As String
strHold = RTrim(pstr)
Do While InStr(strHold, " ") > 0
strHold = Left(strHold, InStr(strHold, " ") - 1) & Mid(strHold, InStr(strHold, " ") + 1)
Loop
onespace = Trim(strHold)
End Function
'------------------------------------------------------------------