PDA

View Full Version : [SOLVED:] Replacing text with Regular expressions



xSmityx
05-13-2021, 09:48 AM
What I'm trying to accomplish here is to replace all speaker identifications from the text below (F6:, F7:, F8:, F9:)

F6: I just I don't,
F7: think that there is a
F8: basis in the law for that argument.
F7: And so our position is the same that

So what the user will do is they will highlight the entirety of the text shown above and want to remove all the speaker identifications F#: Code is below:


Dim oRng As Range
Dim regExp As Object
Dim startingplace As Range
Set regExp = CreateObject("vbscript.regexp")

Application.ScreenUpdating = False


Set oRng = Selection.Range
oRng.Select

Debug.Print Len(Selection.Text)
If Len(Selection.Text) > 1 Then

With regExp
.MultiLine = True
.Pattern = "[A-Za-z0-9\s\.]*\:\s\s"
.Global = True
'Selection.Text = .Replace(oRng.Text, "")
End With
End If



End Result is:

"I just I don't,And so our position is the same that"

So it did the first and the last ID but missed the two middle ones and completely remove those entire lines.

Thanks in advance for any help or direction on this.

macropod
05-13-2021, 02:49 PM
All you need for that is a wildcard Find/Replace, with:

Find = F[0-9]@:(*)^13
Replace = \1
You don't even need a macro!