1

I am trying to find every instance of a pattern in a range of cells and replace those instances with a string literal.

Sub poBoxRegEx(myRange As Range)
    Dim myRegEx As New VBScript_RegExp_55.RegExp
    myRegEx.Pattern = "^[p|P][ |\.]*[o|O|0][\.]* ([b|B]ox[ ]*)"
    myRegEx.ignoreCase = True
    myRegEx.Global = True
    myRegEx.MultiLine = True

    myRange.Value = myRegEx.Replace(myRange.Value, "PO Box ")

End Sub

Now every other topic and use case I find online here and else where is creating a function accessible via the function bar. I don't need that because I am not creating something that parses the text in column A and outputs the result to column B. No, I just want this find and replace to replace the original text in the original cell.

4
  • 1
    Can you provide the context you're originally trying to find? Commented Jul 16, 2014 at 1:46
  • I receive data for mailing lists, which is never consistent. One day I may get a data set with 13 columns, another 50 columns. I never know where "PO Box" (or rather a mangled variant of it) is going to appear. I am looking to replace every instance of inconsistent cases, spaces and dot delimited ("P.O.") with a string literal ("PO Box"). Commented Jul 16, 2014 at 1:52
  • So is your question about your regex or how to do the replacement? Commented Jul 16, 2014 at 1:54
  • Your pattern should be ^[pP][ .]*[oO0][.]* ([bB]ox *) . don't need to give an | inside char class. Commented Jul 16, 2014 at 2:04

1 Answer 1

1

You need a loop if your range is greater than one cell.

Sub poBoxRegEx(myRange As Range)
    Dim myRegEx As New VBScript_RegExp_55.RegExp
    Dim cell As Range
    myRegEx.Pattern = "^[p|P][ |\.]*[o|O|0][\.]* ([b|B]ox[ ]*)"
    myRegEx.ignoreCase = True
    myRegEx.Global = True
    myRegEx.MultiLine = True

    for each cell in myRange
        cell.Value = myRegEx.Replace(cell.Value, "PO Box ")
    next
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Good job! Hours of searching and tinkering... you nailed it in much less time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.