+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    01-05-2010
    Location
    Tel Aviv , Israel
    MS-Off Ver
    Excel 2003
    Posts
    4

    Separate an alphanumeric cell into its components

    Hi,
    I need to separate the content of every address cell to the four separate cells : city, street, street number, zip code

    address cell examples:
    98 ironwood street rochester 48063
    chesterfield 55 street , Chicago 55660
    76 drexler , Bozman , 33087
    Collins 230, Austin
    etc
    etc

    I have a comprehensive city list available:
    Bozeman
    Chicago
    Austin
    rochester
    springfield
    Los Angeles
    etc

    order and location of numbers and texts in address cells is not fixed : 98 ironwood or ironwood 98, city name may appear first etc:
    98 ironwood rochester 48063
    rochester 48063 ironwood 98

    zip code is always 5 digit number
    street number is 3 digit or less
    Can anyone please point me in the right direction?
    Thanks,
    Ziv

  2. #2
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,478

    Re: how to seperate an alphanumeric cell into its components

    lots of manual copy paste would be required unles you have something seperating fieldsfor example is there always a ,(comma) between items?
    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    01-05-2010
    Location
    Tel Aviv , Israel
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: how to seperate an alphanumeric cell into its components

    Quote Originally Posted by martindwilson View Post
    lots of manual copy paste would be required unles you have something seperating fieldsfor example is there always a ,(comma) between items?
    there are usually no commas, just text and numbers. I suppose I could use a function to delete all commas to make it uniform.
    city name is one of a finite list of cities I have within the spreadsheet (perhaps 200 or so). Street name is any other text in the cell (no state or country in the cell). any 5 igit number is zip code. Any 3 or less digit number is street number.

  4. #4
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,478

    Re: how to seperate an alphanumeric cell into its components

    attach an example of each variation,
    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Registered User
    Join Date
    01-05-2010
    Location
    Tel Aviv , Israel
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: how to seperate an alphanumeric cell into its components

    address cell examples:
    98 ironwood street rochester 48063
    chesterfield 55 street , Chicago 55660
    76 drexler , Bozman , 33087
    Collins 230, Austin

    see attached spreadsheet

    Thanks
    Attached Files Attached Files

  6. #6
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: how to seperate an alphanumeric cell into its components

    try...
    Code:
    Sub ptest()
    Dim RegEx As Object
    Dim TestStr$, cell As Range
        TestStr = Range("A3").Text
        
        For Each cell In Range("B8:B12")
        TestStr = cell.Value
        Set RegEx = CreateObject("vbscript.regexp")
    
        With RegEx
            .MultiLine = False
            .Global = False
            .IgnoreCase = True
            .Pattern = "(.*)(Chicago|rochester|Bozman|Austin|San Diego)(.*|$)" '".*|(.*(\d{2,3}\s)|(\d\s).*).*|.*(.*(\d{5}).*).*|.*(.*(Chicago|rochester|Bozman|Auston).*)" '
            If .Test(TestStr) = True Then
           cell.Offset(0, 1) = .Replace(TestStr, "$2")
          End If
          TestStr = .Replace(TestStr, "$1$3")
         .Pattern = "(.*)(\d{5})(.*|$)"
            If .Test(TestStr) = True Then
            cell.Offset(0, 4) = .Replace(TestStr, "$2")
            End If
            TestStr = .Replace(TestStr, "$1$3")
           .Pattern = "(^|.*|\s)(\d+)(\s|.*|$)"
           If .Test(TestStr) = True Then
          cell.Offset(0, 3) = .Replace(TestStr, "$2")
            End If
       cell.Offset(0, 2) = .Replace(TestStr, "$1" & " " & "$3")
    End With
    Next cell
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  7. #7
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: how to seperate an alphanumeric cell into its components

    this works with the example
    Code:
    Sub ptest()
    Dim RegEx As Object, matches As Object, match As Object
    Dim TestStr$, cell As Range
       For Each cell In Range("B8:B12")
        TestStr = cell.Value
        Set RegEx = CreateObject("vbscript.regexp")
    
        With RegEx
            .MultiLine = False
            .Global = True
            .IgnoreCase = True
            .Pattern = "(.*)(Chicago|rochester|Bozman|Austin|San Diego)(.*|$)" '".*|(.*(\d{2,3}\s)|(\d\s).*).*|.*(.*(\d{5}).*).*|.*(.*(Chicago|rochester|Bozman|Auston).*)" '
            If .Test(TestStr) = True Then
           cell.Offset(0, 1) = .Replace(TestStr, "$2")
          End If
          TestStr = .Replace(TestStr, "$1$3")
         .Pattern = "(.*)(\d{5})(.*|$)"
            If .Test(TestStr) = True Then
            cell.Offset(0, 4) = .Replace(TestStr, "$2")
            End If
            TestStr = .Replace(TestStr, "$1$3")
         .Pattern = "\d+"
           If .Test(TestStr) = True Then
          Set matches = .Execute(TestStr)
            For Each match In matches
           cell.Offset(0, 3) = match
            Next
              End If
             .Pattern = "\d+"
           If .Test(TestStr) = True Then
          cell.Offset(0, 2) = Trim(.Replace(TestStr, ""))
            End If
     End With
    Next cell
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  8. #8
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: how to seperate an alphanumeric cell into its components

    and if there is no street address
    Code:
    Sub ptest()
        Dim RegEx As Object, matches As Object, match As Object
        Dim TestStr$, cell As Range
        For Each cell In Range("B8:B12")
            TestStr = cell.Value
            Set RegEx = CreateObject("vbscript.regexp")
            With RegEx
                .MultiLine = False
                .Global = True
                .IgnoreCase = True
                .Pattern = "(.*)(Chicago|rochester|Bozman|Austin|San Diego)(.*|$)"
                If .Test(TestStr) = True Then
                    cell.Offset(0, 1) = .Replace(TestStr, "$2")
                    TestStr = .Replace(TestStr, "$1$3")
                End If
                .Pattern = "(.*)(\d{5})(.*|$)"
                If .Test(TestStr) = True Then
                    cell.Offset(0, 4) = .Replace(TestStr, "$2")
                    TestStr = .Replace(TestStr, "$1$3")
                 End If
                .Pattern = "\d+"
                If .Test(TestStr) = True Then
                    Set matches = .Execute(TestStr)
                    For Each match In matches
                        cell.Offset(0, 3) = match
                    Next
                End If
                If .Test(TestStr) = True Then
                    cell.Offset(0, 2) = .Replace(TestStr, "")
                Else
                    cell.Offset(0, 2) = TestStr
                End If
            End With
        Next cell
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

  9. #9
    Forum Moderator pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,141

    Re: how to seperate an alphanumeric cell into its components

    If you every come back added code to remove the comas
    Code:
    Option Explicit
    Sub ptest()
        Dim RegEx As Object, matches As Object, match As Object
        Dim TestStr$, cell As Range
        For Each cell In Range("B8:B12")
            TestStr = cell.Value
            Set RegEx = CreateObject("vbscript.regexp")
            With RegEx
                .MultiLine = False
                .Global = True
                .IgnoreCase = True
                .Pattern = "(.*)(Chicago|rochester|Bozman|Austin|San Diego)(.*|$)"
                If .Test(TestStr) = True Then
                    cell.Offset(0, 1) = .Replace(TestStr, "$2")
                    TestStr = .Replace(TestStr, "$1$3")
                End If
                .Pattern = "(.*)(\d{5})(.*|$)"
                If .Test(TestStr) = True Then
                    cell.Offset(0, 4) = .Replace(TestStr, "$2")
                    TestStr = .Replace(TestStr, "$1$3")
                 End If
                .Pattern = "\d+"
                If .Test(TestStr) = True Then
                    Set matches = .Execute(TestStr)
                    For Each match In matches
                        cell.Offset(0, 3) = match
                    Next
                End If
                If .Test(TestStr) = True Then
                    
                    cell.Offset(0, 2) = .Replace(TestStr, "")
                 cell.Offset(0, 2) = Replace(TestStr, ",", "")
                Else
              
                    cell.Offset(0, 2) = Replace(TestStr, ",", "")
                End If
            End With
        Next cell
    Set RegEx = Nothing: Set matches = Nothing
    End Sub
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    JBeaucaires Excel Files

    VBA for smarties - snb

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0