+ 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

    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

  2. #2
    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.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    07-13-2007
    Posts
    151
    Correct. I was testing both methods. I prefer the array and maintaining the list in the code for this particular application.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    =Foox(A1,B1:B100) As String
    where B1:B100 has a list of surfix
    Function Foox(txt As String, rng As Range) As String
    Dim myPtn As String
    myPtn = Join(Evaluate("transpose(" & rng.Address & ")"),"|")
    With CreateObject("VBScript.RegExp")
        .Pattern = ".*\d+(\D+)\s(" & myPtn & ")\s.*"
        .IgnoreCase = True
        Foox = .replace(txt,"$1")
    End With
    End Fuction
    Last edited by jindon; 02-18-2008 at 03:51 AM.

+ 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