3

I'm trying to get a number of character occurence in a string. It should work, but it doesn't.

Function countLetter(letter As String, secretWord As String)
    MsgBox (Split(secretWord, letter).Length)
    countLetter = Split(secretWord, letter).Length - 1
End Function

What is wrong here?

1 Answer 1

7

Split does not have a .Length property. Use UBound for a zero-based count of the elements in the array.

Function countLetter(letter As String, secretWord As String)
    MsgBox UBound(Split(secretWord, letter)) + 1
    countLetter = UBound(Split(secretWord, letter))
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

@Ans unrelated to the splitting, but notice the lack of parentheses around the Message argument passed to the MsgBox function; whenever you invoke a procedure without capturing its return value into a local variable, you should not have parentheses around the arguments. This will cause you problems one day or another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.