Count or find duplicates in selection

S

StephCA

Hi: Word97/2003 VBA
I need to see if there are two left brackets " [ " in selected text. Can I
do this without using Find -- just return the count of left brackets in the
selection? Can someone please give me code or a starting point? TIA.

More info:
I've got code to find and select bracketed text: [all this gets selected,
including the brackets]

Problem = Some text may be missing the right bracket: [this needs a right
bracket. [Here's text with proper brackets.].
In above case, everything from the FIRST "[" through the "]" gets selected
and it'll contain two left brackets. I want to look in the selection and find
out if it has two left brackets--(in which case I have an error).

Thanks much. Stephanie
 
H

Helmut Weber

Hi Stephanie,

something along these lines:

Sub Testxxx4()
Dim s As String
Dim i As Long
s = Selection.Text
While InStr(s, "[") > 0
i = i + 1
s = Right(s, Len(s) - InStr(s, "["))
Wend
MsgBox i
End Sub

or as a function:

Public Function MyCount(s1 As String, s2 As String) As Long
Dim i As Long
While InStr(s1, s2) > 0
i = i + 1
s1 = Right(s1, Len(s1) - InStr(s1, s2))
Wend
MyCount = i
End Function
' -------------------------------------------------------
Sub MyTest4()
MsgBox MyCount(Selection.Text, "[")
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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