Yes I was thinking the same and I've found the following code:
Locate nth occurrence of a string
If you want to find the nth occurrence of a string, not simply the first one, set n to the desired number and run the following code:
position = 0
position = InStr(position + 1, "avbprogramvb", "vb")
n = n - 1
Loop Until position = 0 Or n = 0
The loop keeps searching until the nth occurrence is found or there was no match. position tells the result.
To search for the nth occurrence after a certain start position, let position = start - 1.
I'm just having trouble getting it to work myself. I'm very rusty with my VB and I was never that fluent to begin with. Could you please show me an example of the routine you've suggested? It sounds slightly different to the above code I've posted.
Extra code added for illustration.
Shows input string, position counts, output string.
Private Sub Command1_Click()
Dim s As String
Dim o As String
Dim p As Integer, cnt As Integer
Dim x As String ' for illustration
s = "abacadaeafagahaiajakalamanaoapaqarasatauavawaxayaza"
x = Space$(Len(s)) ' for illustration
o = s 'o is the output string
p = InStr(o, "a") 'find the first "a"
cnt = 0
Do While p <> 0 'Do while we've found an "a"
cnt = cnt + 1 ' count how many "a" found
Mid$(x, p, 1) = Right$(Str$(cnt), 1) ' for illustration, insert cnt into the space string
If cnt = 4 Then ' If we've found a fourth "a" then
Mid$(o, p, 1) = "b" ' Overwrite the "a" with a "b"
cnt = 0 ' reset our counter
End If ' End If
p = InStr(p + 1, o, "a") ' Find the next "a"
Debug.Print s 'Print the source string
Debug.Print x 'Print the position of "a"s in the source string
Debug.Print o 'print the output string
Awesome! That looks exactly like what I'm after. I'll test it tonight (GMT 10+) and let you know how I go, but it looks spot on from a glance. Thank you very, very much for helping me out with this, I really appreciate it
The commenting and results field were nice touches also. Thank you for going out of your way.
Have a great day! (it's 9:45AM here in Eastern Australia).