+ Reply to Thread
Results 1 to 13 of 13

Remove Certain Last Words from String

Hybrid View

  1. #1
    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.

  2. #2
    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

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    An array would work. Since MATCH works with arrays, you could avoid looping. Match is also case insensitive.

    Dim suffixArray as Variant
    suffixArray = Array("Street", "St.", "St", "Lane, "Ln.")
    
    If IsNumeric (Application.Match(lastWord, suffixArray,0) Then
       Msgbox "on final word list"
    Else
       Msgbox "not on list"
    End If
    Last edited by mikerickson; 02-16-2008 at 11:02 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    07-13-2007
    Posts
    151
    Thanks Mikerickson. That's the kind of thing I was envisioning. I had made similar attempts but never quite got my syntax correct I guess.

    Thanks for pointing out that MATCH is is not case sensitive. It looks like this will do the trick for me.

    Shred

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

    Jindon

    Wasn't unhappy...just didn't click with me. Maybe it was way over my head.

    Thanks for replying.

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

    Solution Found

    Thanks Mikerickson for pointing me in this direction. I adapted your ideas into some code that eventually worked. I was unable to get the results I desired using ISNUMERIC when there was not a match as an error would be returned by the Match worksheet function; that worked fine if is there was a match between the lastword and the list of choices. So I resorted to ISERROR instead and got the results I needed. It now works whether there is a match or not.

    I also got it working with a dynamic range pulled from the workbook ('commented line)

    See code below.

    Thanks again.

    Private Sub CommandButton7_Click()
    Dim mystr, mynewstr, lastword As String
    Dim lastblank, firstblank As Long
    Dim suffixarray()
    
    mystr = Cells(ActiveCell.Rows.Row, 1).Value
    firstblank = InStr(mystr, " ")
    lastblank = InStrRev(mystr, " ")
    suffixarray() = Array("Street", "Drive", "Lane")
    
    lastword = Right(mystr, Len(mystr) - lastblank)
    
    On Error Resume Next
    '    If IsError(Application.WorksheetFunction.Match(lastword, Range("suffixlist"), 0)) Then
        If IsError(Application.WorksheetFunction.Match(lastword, suffixarray, 0)) Then
            mynewstr = Right(mystr, Len(mystr) - firstblank)
          Else
            mynewstr = Mid(mystr, firstblank + 1, lastblank - firstblank - 1)
        End If
    On Error GoTo 0
      
      MsgBox mystr & vbCrLf & vbCrLf & mynewstr
    
    End Sub

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    You don't need both the named range and the array. Managing this list would be much easier using only one or the other, but not both. It sounds like you would prefer the array.

+ 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