so im reading VBA Developer's Handbook, 2nd edition and i have a question about a function (why it's returning 0, when it should be greater than 0). see below:
Public Function dhCountInCaseS(strText As String, strFind As String, Optional lngCompare As VbCompareMethod = vbBinaryCompare) As Long
Dim lngCount As Long
Dim lngPos As Long
'this search is case sensitive so "A" is not the same as "a"
'if there's nothing to find, there surely can't be an found so return 0
If Len(strFind) > 0 Then
lngPos = 1
Do
lngPos = InStr(lngPos, strText, strFind, lngCompare)
If lngPos > 0 Then
lngCount = lngCount + 1
lngPos = lngPos + Len(strFind)
End If
Loop While lngPos > 0
Else
lngCount = 0
End If
dhCountIn = lngCount
End Function
and here is the procedure
Sub Count_Vowels()
Dim strText As String
Dim intVowels As Long
strText = "THIS IS A TEST OF THE NATIONAL BROADCASTING SYSTEM"
intVowels = dhCountInCaseS(strText, "A") + dhCountInCaseS(strText, "E") + dhCountInCaseS(strText, "I") + _
dhCountInCaseS(strText, "O") + dhCountInCaseS(strText, "U")
Debug.Print intVowels
End Sub
Is there a problem with the function or how i'm implementing the procedure?
Bookmarks