1

I am trying to do some data clean up and could use some help with the replace function.

If I need to replace a cells value in excel from lets say "Paul the Optometrist Pa" to "Paul the Optometrist PA" I would just use something like this

Replace(t, " Pa", " PA")

The problem with that is it also changes the letters in Paul to "PAul". Obviously I do not want that. Is there a way to keep it from changing anything in the string that isn't specifically the "PA" with nothing but spaces possible before and after it?

1 Answer 1

1

This RegEx will match Pa in the string and replace it with PA. This can be easily updated to loop through your data set

Sub RegExExample()               
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = False
        .Pattern = "(Pa)\b"

        MsgBox .Replace("Paul the Optometrist Pa", "PA")
    End With
End Sub

Will output Paul the Optometrist PA

Sign up to request clarification or add additional context in comments.

5 Comments

I don't quite follow this logic; since the string "Paul the Optometrist Pa" contains Paul and Pa with the same case structure. So surely case sensitivity is not relevant?
That's still going to produce PAul the Optometrist PA. OP is likely going to need to use regex with word borders to do this (I know what, I just don't know how)
@CLR Actually it works fine for me, it's just the reasoning is incorrect. ?Replace("Paul the Optometrist Pa", " Pa", " PA", compare:=vbBinaryCompare) prints Paul the Optometrist PA
Yea it would only because my example was bad. There is no space in front of Paul which is why it won't change it to PAul. if the value was "hello Paul Pa" it would still give me the same problem.
@Tom Yep that is the winner! Thanks for the help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.