counting a character

L

Larry

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?

Larry
 
K

Klaus Linke

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
 
L

Larry

Oops, I'm a Word 97 user. Maybe it is time for me to upgrade after all,
with all those expanded vba capabilities in the newer versions. But I
hate the thought of it.

Larry
 
H

Helmut Weber

Hi Larry,
try this:
Sub Test115()
Dim dlgtest As Dialog
Set dlgtest = Dialogs(wdDialogEditReplace)
On Error GoTo Message
dlgtest.Display
Message:
MsgBox Err.Description
End Sub
Err.Description reveals, astongishingly enough,
the number of replacements.
And this is the number:
hallo = Err.Description
For I = 1 To Len(hallo)
If IsNumeric(Mid(hallo, I, 1)) Then a = a & Mid(hallo,
I, 1)
Next I (By Andi Mayer)
Msgbox a
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
 
L

Larry

But it's neat to know that in Word 2000 there is this other way besides
Find/Replace of replacing individual characters.
 
K

Klaus Linke

Larry said:
Oops, I'm a Word 97 user. Maybe it is time for me to
upgrade after all, with all those expanded vba capabilities
in the newer versions. But I hate the thought of it.

Larry


Oops!

You could use for example

-- loop all characters in myString, and use Mid(myString,i,1) to check if
it's equal to the character you are looking for.

-- or, determine the next occurrance of the character with Instr, until no
more occurrances are found.

In both cases, you could use a counter variable to count how often the
character appears.

I just wrote two such macros, but my stupid machine froze (no, wrong word:
burnt out on account of excessively hot weather) just as I was about to
post.

Perhaps you can try yourself. If you run into problems, post back.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top