Link to home
Start Free TrialLog in
Avatar of David L. Hansen
David L. HansenFlag 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?
SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David L. Hansen

ASKER

Thanks!
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
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.