Hello, fellow coders,
I've gotten some powerful code from here, yet I've got Soooo much to learn. Can anyone tell me how to make the following code stop at each find, instead of shooting through the whole search without stopping? This code simply totals up the amount of times the search text was found and turns them all red. I expect it to be simple, but simple is hard when you don't know what it is. Thanks in advance.
Option Explicit
Option Compare Text
Private Sub Command2_Click()
unload me
End Sub
Private Sub Form_Load()
RichTextBox1.LoadFile "MyFile.wav"
End Sub
Private Sub Command1_Click()
Dim strval As String 'Inputbox returns a string
Dim nStrings As Long
RichTextBox1.LoadFile "MyFile.wav"
strval = " " & InputBox("Enter the string to find.", "Findit", _
"Love") & " "
If strval <> "" Then
nStrings = FindIt(RichTextBox1, strval)
MsgBox (Str$(nStrings + 1) & " instances of """ & strval & """ found.")
End If
End Sub
Private Function FindIt(Box As RichTextBox, Srch As String, _
Optional Start As Long)
Dim retval As Long 'Instr returns a long
Dim Source As String 'variable used in Instr
Source = Box.Text 'put the text to search into the variable
If Start = 0 Then Start = 1 'the initial call doesn't pass a value
'for Start, so it will equal 0
retval = InStr(Start, Source, Srch)
If retval <> 0 Then
With Box
.SelStart = retval - 1
.SelLength = Len(Srch)
.SelColor = vbRed
.SelBold = True
.SelLength = 0
End With
Start = retval + Len(Srch)
FindIt = 1 + FindIt(Box, Srch, Start)
End If
End Function
