It can be corrected as follows:
But I would not use a Split to count the number of characters in a string.Code:Public Function CountChrInString(ByVal Expression As String, ByVal Character As String) As Long ' Returns the count of the specified character in the specified string. ' Dim lngResult As Long Dim strParts() As String ' If Len(Expression) > 0 Then strParts = Strings.Split(Expression, Character) lngResult = UBound(strParts, 1) If (lngResult = -1) Then lngResult = 0 End If CountChrInString = lngResult End If End Function
Code:Public Function CharCount(sString As String, sChar As String) As Long Dim lPos As Long If Len(sString) = 0 Then Exit Function lPos = 0 ' CharCount= 1 Do lPos = InStr(lPos + 1, sString, sChar , vbBinaryCompare) If lPos > 0 Then CharCount= CharCount+ 1 Loop Until lPos = 0 End Function




Reply With Quote