Link to home
Start Free TrialLog in
Avatar of frankmorrison
frankmorrison

asked on

Find a number then replace characters before it in a string

Hello Gurus,

I have a textbox that contains something like "blablablabla777.JPG". I'm trying find the first number in that string and them replace whatever is before the first number with the text "slide", so that It would say "slide777.JPG".  For some reason, the code that I have finds the last number in "blablablabla777.JPG", then it replaces whatever is before that last "7" including the two 7s before it.  Here's the code:

Private Sub Command1_Click()
For i = 1 To 9
    pos = InStr(Text1.Text, i & ".JPG")

    If (pos <> 0) Then
        charsToReplace = Mid(Text1.Text, 1, pos - 1)
        Text1.Text = Replace(Text1.Text, charsToReplace, "slide")
        Exit Sub
    End If
Next i
End Sub


Can you guys please help?

Thanks in advance.

JH.
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of frankmorrison
frankmorrison

ASKER

Thanks a lot bobbit31!