+ Reply to Thread
Results 1 to 3 of 3

Delimit city, state, zip if there are multiple commas

  1. #1
    Forum Contributor
    Join Date
    01-28-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    105

    Delimit city, state, zip if there are multiple commas

    I want to delimit this into city, state, and zip.
    Unfortunately there are sometimes more than 1 comma for example the last line in the below example.
    As a result I want to delimit from the right. I was wondering if this is possible/ any other alternative to separate the data.

    Matthews, TX 7508175081
    Glen Lexington, TX 7524375243
    San Felipe Houston, TX 77057
    First Avenue, New York, NY 10016

  2. #2
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,881

    Re: Delimit city, state, zip if there are multiple commas

    When working with addresses and delimiting, there are always going to be exceptions. I try to use the 90/10 rule. If I use the standard Text-to-columns function and it works for at least 90% of the data, I'll manually update those that failed.

    If, of those that failed, a majority are for one reason, I will build that on top of the Text-to-columns (use a macro instead) and leave the manual work to a very small percentage.

    In your example, who is to say that there won't be two commas next to each other, or a third comma between the state and zip? Or no commas? There's a number of possibilities, and while many can be checked and coded for, the time spent trying to cover all the exceptions can easily exceed the time spent getting most done quickly and manually fixing a few exceptions.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Delimit city, state, zip if there are multiple commas

    Hello undergraduate,

    I modified a macro I wrote about 2 years to parse both US and Canadian mail addresses. This macro requires that the city be followed by at least one comma, the state is abbreviated according to US Postal standards, and the Zip can be 5 digits or 9 (Zip + 4). The Zip + 4 can be separated by a hyphen, a space or neither.

    The macro assumes the addresses are in column "A" of the ActiveSheet. The city will be in "A", the state in "B", and the Zip in "C" after the macro runs. If the macro does not recognize the address according to its rules (see above) then it remains untouched.

    Please Login or Register  to view this content.

    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Last edited by Leith Ross; 11-19-2010 at 07:47 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1