+ Reply to Thread
Results 1 to 13 of 13

Remove Certain Last Words from String

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-13-2007
    Posts
    151

    Remove Certain Last Words from String

    I'm attempting to extract the Street Names from an Address.

    For Example, if given "123 Very Big Street" I'd want to extract "Very Big".

    If given, "123 Very Big" I'd want the same answer.

    I've written code to do this, with a simple IF statement to identify mathcing last words like "Street" and "Drive".

    How can I efficiently do this with a large list of last words? I'm drawing a blank on how to query against an array that would contain all these values.

    Thanks,

    Shred

    Private Sub CommandButton7_Click()
    Dim mystr, mynewstr, lastword As String
    Dim lastblank, firstblank As Long
    
    mystr = Cells(ActiveCell.Rows.Row, 1).Value
    firstblank = InStr(mystr, " ")
    lastblank = InStrRev(mystr, " ")
    
    lastword = Right(mystr, Len(mystr) - lastblank)
    
    If firstblank = lastblank Then
        mynewstr = mystr
    Else
        If LCase(lastword) = "drive" Or LCase(lastword) = "street" Then
            mynewstr = Mid(mystr, firstblank + 1, lastblank - firstblank - 1)
        Else
            mynewstr = Right(mystr, Len(mystr) - firstblank)
        End If
    End If
    
    MsgBox mystr & vbCrLf & vbCrLf & mynewstr
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    Deleted...
    Last edited by jindon; 02-16-2008 at 01:45 AM.

  3. #3
    Forum Contributor
    Join Date
    07-13-2007
    Posts
    151
    What I was getting at is having about 50 or so strings to match against.

    Street, Drive, Way, Lane, road, rd., st., Blvd., Avenue, Ave, etc.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You might want to put the list of final words in a dynamic named range, so it can be added to.

    Code like this could be used for the test.
    If IsNumeric (Application.Match(LCase(lastWord), Range(myNamedRange),0) Then
       Msgbox "on final word list"
    Else
       Msgbox "not on list"
    End If
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    Edited
    You don't seem to be happy with the UDF, so I delete.
    Last edited by jindon; 02-16-2008 at 01:19 AM.

  6. #6
    Forum Contributor
    Join Date
    07-13-2007
    Posts
    151

    mikerickson

    Thanks for the reply.

    I'm trying to keep the maintenance of the list of lastwords out of the user's hands. I want to keep it in the code. Either way, I have to type the values somewhere.

    I was thinking I could somehow populate an array with the values, and then loop through them to see if I have a match with the curent string.

    I guess I could hide the worksheet the dynamic range list is on.

    Any other ideas?

    Thanks,

    Shred

+ 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