InStr only finds first occurrence of a match... how to i find them all?

mrlemmer11

New Member
Joined
Jun 8, 2015
Messages
32
Hello,

I am using inkEdit to change the color of all strings in my inkEdit textbox " SAVED " to be green. Only problem is that it will only make the first occurrence green and not any of the others. How do i fix this? thanks!

Code:
Private Sub inkChange()
Dim iStart
iStart = InStr(formLOB3.txtLog.Text, " SAVED ")
    If iStart <= 0 Then
        Exit Sub
    End If
With formLOB3.txtLog
    .SelStart = iStart - 2
    .SelLength = 7
    .SelBold = True
    .SelFontSize = 8
    .SelColor = RGB(0, 128, 0)
End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hello,

I am using inkEdit to change the color of all strings in my inkEdit textbox " SAVED " to be green. Only problem is that it will only make the first occurrence green and not any of the others. How do i fix this? thanks!

Code:
Private Sub inkChange()
Dim iStart
iStart = InStr(formLOB3.txtLog.Text, " SAVED ")
    If iStart <= 0 Then
        Exit Sub
    End If
With formLOB3.txtLog
    .SelStart = iStart - 2
    .SelLength = 7
    .SelBold = True
    .SelFontSize = 8
    .SelColor = RGB(0, 128, 0)
End With
End Sub

Bump
 
Upvote 0
You can create a function which returns an array containg the starting positions of all the string occurrences
Something like this :
Code:
Function InstrEx(ByVal SearchedString As String, _
        ByVal SoughtString As String, _
        Optional ByVal Compare As VbCompareMethod _
        ) As Long()
        
    Dim arSplit() As String
    Dim arStringPositions() As Long
    Dim lCounter As Long
    Dim lStart As Long
    
    If Compare = 0 Then Compare = vbTextCompare
    arSplit = Split(SearchedString, SoughtString)
    ReDim arStringPositions(UBound(arSplit))
    For lCounter = 1 To UBound(arSplit)
        lStart = InStr(1, SearchedString, SoughtString, Compare)
        SearchedString = Replace(SearchedString, SoughtString, String(Len(SoughtString), Chr(1)), 1, 1)
        arStringPositions(lCounter) = lStart
    Next
    InstrEx = arStringPositions
    Erase arStringPositions
End Function

And to use it in your inkEdit code you can proceed like this :
Code:
Private Sub inkChange()
    Dim ar() As Long
    Dim i As Long
    
    ar = InstrEx(formLOB3.txtLog.TEXT, " SAVED ", vbTextCompare)
    If UBound(ar) > 0 Then
        For i = 1 To UBound(ar)
            With formLOB3.txtLog
                .SelStart = ar(i) - 2
                .SelLength = 7
                .SelBold = True
                .SelFontSize = 8
                .SelColor = RGB(0, 128, 0)
            End With
        Next
    End If
End Sub
 
Upvote 0
I am using inkEdit to change the color of all strings in my inkEdit textbox " SAVED " to be green. Only problem is that it will only make the first occurrence green and not any of the others. How do i fix this?

Try the following:
Code:
Private Sub inkChange()
    Dim iStart [COLOR=#ff0000]As Long
[/COLOR]    [COLOR=#ff0000]iStart = 1
    Do
[/COLOR]        iStart = InStr([COLOR=#ff0000]iStart,[/COLOR] formLOB3.txtLog.Text, " SAVED ")
        If iStart = 0 Then Exit [COLOR=#ff0000]Do
[/COLOR]        With formLOB3.txtLog
            .SelStart = iStart - 2
            .SelLength = 7
            .SelBold = True
            .SelFontSize = 8
            .SelColor = RGB(0, 128, 0)
        End With
        [COLOR=#ff0000]iStart = iStart + 7[/COLOR]    ' 7 = len(" SAVED ")
    [COLOR=#ff0000]Loop
[/COLOR]End Sub

Arguably, we could declare iStart to be type Integer. I use type Long as a "good habit". There is no performance penalty. There is little storage penalty for individual variables. I might consider type Integer for a large array.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,957
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top