3

I have some regex knowledge from using it JavaScript but now I want to use it in Excel and VBA and I have some problems getting it work.

I want to replace >abc with <br>> abc.

In JavaScript I use >([a-zA-Z0-9]) and <br>> $1, but here I don't know how to do it.

Also I want to use it with Range.Replace function, like:

oRange.Replace What:=">abc", Replacement:="<br>> abc", MatchCase:=False

Any help will be appreciated.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
a.m
  • 169
  • 3
  • 11

1 Answers1

-1

Maybe something like this routine. This will search a specified range with the pattern and replace the contents...

Hope that helps.

Public Sub RegExMatchAndReplace(ByVal Pattern As String, ByVal Data As Range, ByVal Replace As String)


Dim oRegEx As New RegExp
Dim rPtr As Range

For Each rPtr In Data
    With oRegEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = Pattern
        rPtr.Value = oRegEx.Replace(rPtr.Value, Replace)
    End With
Next

End Sub
PaulG
  • 1,051
  • 7
  • 9