Nelson78
Well-known Member
- Joined
- Sep 11, 2017
- Messages
- 526
- Office Version
- 2007
Hello everybody.
I've been manipulating some strings.
Now, I'm in a deadlock.
I mean: in a specific string, I want to replace only the third occurrence of character "a" with character "b".
How can I build the pattern (see in red below).
I've been manipulating some strings.
Now, I'm in a deadlock.
I mean: in a specific string, I want to replace only the third occurrence of character "a" with character "b".
How can I build the pattern (see in red below).
Code:
Sub substitute()
Dim lr As Long
Dim Rng As Range, Cell As Range
Dim replacement As Object
lr = Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = Range("A1:A" & lr)
Set replacement = CreateObject("vbscript.regexp")
With replacement
[COLOR="#FF0000"].Pattern =[/COLOR]
.Global = True
.IgnoreCase = True
End With
For Each Cell In Rng
Cell.Value = replacement.Replace(Cell.Value, "b")
Next Cell
End Sub
Last edited: