Avatar of David L. Hansen
David L. Hansen
Flag for United States of America asked on

Conditionally edit cells in an Excel range using VBA

I have a list of P.O. Boxes and I need to detect and remove duplicates.  Of course the column contains P.O. Boxes with all sorts of formats (ie. "po box," "p.o. Box," "Po Box," "P.O. Box," "P. O. Box," etc.).  If I normalize all of them to the same format I will easily be able to remove the dups.  I thought I'd first remove all periods in the column, then apply UPPER to get them all to uppercase.  Then remove all spaces and look for dups.  I would probably add a space back between the "O" and the following numbers.

Advice?
Microsoft ExcelMicrosoft Access

Avatar of undefined
Last Comment
David L. Hansen

8/22/2022 - Mon
SOLUTION
Ken Butters

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
David L. Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
David L. Hansen

ASKER
Thanks!
byundt

You might want to consider using a user-defined function to standardize your PO Boxes. You install the code in a regular module sheet (just like a macro), then use it with a formula like:
=PO_Boxer(A1)

Function PO_Boxer(s As String) As String
Dim RgExp As Object
Set RgExp = CreateObject("VBScript.RegExp")
With RgExp
    .Pattern = "([Pp])(\s*)(\.?\s*)([Oo])(\s*)(\.?\s*)([Bb][Oo][Xx])(\s*)(\d+)"
    .Global = True
    PO_Boxer = .Replace(s, "P.O. Box $9")
End With
Set RgExp = Nothing
End Function

Open in new window


Brad
PO-Boxer-Q27732653.xls
David L. Hansen

ASKER
I found a different formula that took care of my problem.  I still want to award points to the expert though because his formula would have worked just as well I believe.  He also gave me a much needed tip at the end.
Your help has saved me hundreds of hours of internet surfing.
fblack61