Larry said:
Without using Find/Replace (whether manually or in a macro), is there a
vba way of determining how many times a certain character appears in a
selection?
Hi Larry,
In Word2000+, I'd either
-- determine the length (Len) of the selected text, use Replace to replace
the character with nothing, and determine the length again.
Dim myString As String
' ... you'd use Selection.Text instead of myString ...
myString = "This is a test with 6 blanks!"
MsgBox Len(myString) - Len(Replace(myString, " ", "")), _
vbInformation, "Number of blanks:"
Or ...
-- use Split on the selected text, with the character as the delimiter. You
get the number of occurrances through the upper bound of the resulting
array:
Dim myString As String
Dim myArray
myString = "This is a test with 6 blanks!"
myArray = Split(myString, " ")
MsgBox UBound(myArray),vbInformation,"Number of blanks:"
Don't know what is faster -- or whether it's worth worrying about ;-)
Regards,
Klaus