Sub CountCharacters(Text As String)
Dim iVowel As Integer
Dim iCons As Integer
Dim iNum As Integer
Dim iSpace As Integer
Dim iOther As Integer
Dim cnt As Integer
Dim sChar As String
For cnt = 1 To Len(Text)
sChar = LCase(Mid(Text, cnt, 1))
Select Case sChar
Case "a", "e", "i", "o", "u" ' And sometimes "y"?
' Vowel
iVowel = iVowel + 1
Case "a" To "z"
' Includes the first case, but VB matches the first case and stops
iCons = iCons + 1
Case "0" To "9"
' Numbers
iNum = iNum + 1
Case " ", vbTab, vbLf
' Space characters
' Not including vbCr as this will likely be dealing with Windows text
' and I only want to match one character for a newline
iSpace = iSpace + 1
Case Else
If sChar <> vbCr Then iOther = iOther + 1
End Select
Next cnt
Debug.Print "Text: " & Text
Debug.Print "Vowels: " & CStr(iVowel)
Debug.Print "Consonants: " & CStr(iCons)
Debug.Print "Numbers: " & CStr(iNum)
Debug.Print "Space characters: " & CStr(iSpace)
Debug.Print "Other: " & CStr(iOther)
End Sub
203 views
Usually answered in minutes!
×