+ Reply to Thread
Results 1 to 10 of 10

delete text from cell after a certain word

Hybrid View

nervous_pilchard delete text from cell after a... 01-21-2011, 08:38 AM
arthurbr Re: delete text from cell... 01-21-2011, 08:47 AM
Marcol Re: delete text from cell... 01-21-2011, 08:58 AM
DonkeyOte Re: delete text from cell... 01-21-2011, 09:05 AM
arthurbr Re: delete text from cell... 01-21-2011, 09:07 AM
Marcol Re: delete text from cell... 01-21-2011, 09:18 AM
DonkeyOte Re: delete text from cell... 01-21-2011, 09:24 AM
DonkeyOte Re: delete text from cell... 01-21-2011, 09:28 AM
Marcol Re: delete text from cell... 01-21-2011, 09:28 AM
nervous_pilchard Re: delete text from cell... 01-21-2011, 11:28 AM
  1. #1
    Registered User
    Join Date
    12-20-2010
    Location
    Southampton, England
    MS-Off Ver
    Excel 2007
    Posts
    14

    delete text from cell after a certain word

    Hi, i have a spreadsheet with 100's of rows of data.

    I have found a piece of code that removes text from a cell after the symbol ">" which is as follows:

    Sub test()
    Dim x As String
    With ActiveSheet.UsedRange
        x = .Address
        .Value = Evaluate("if(" & x & "<>"""",if(isnumber(find("">""," & x _
            & ")),left(" & x & ",find("">""," & x & ")-1)," & x & "),"""")")
    End With
    End Sub
    What i require is a slight modification so that it removes all text in the cell onwards from the word "is" - eg.

    This is what the cell contains:
    Windows Service SurfControl Web Filter Version 5.5 Service Pack 2 Service is Up

    I require the following to be displayed:
    Windows Service SurfControl Web Filter Version 5.5 Service Pack 2 Service

    The reason i want the deletion from the word "is" because there are many different phrases that the cell contains after the word 'is' - e.g. is Up, is Down, is Critical etc etc..

    Of course a find and replace option isnt suitable due to the volume of data. A piece of code is ideal so that it can be linked to a macro.

    Any advice?
    Last edited by nervous_pilchard; 01-21-2011 at 11:28 AM. Reason: solved!

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: delete text from cell after a certain word

    Of course a find and replace option isnt suitable due to the volume of data.
    I don't see why? Selecting the data then Ctrl+H

    Find what: is*
    Replace with: (leave blank)
    Replace all

    Does it take that long?

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: delete text from cell after a certain word

    Perhaps it is not an issue but

    Find what: is * ("is" space "*")

    Might be safer

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: delete text from cell after a certain word

    Purely for sake of Evaluate demo. you could modify to:

    .Value = Evaluate("IF(ISTEXT(" & x & "),LEFT(" & x & ",FIND("" is "","" ""&" & x & "&"" is "")-2),REPT(" & x & ",1))")
    again this assumes to always look for <space>is<space> to preclude false positives
    (we add " is " at the end of each string to avoid need for error handling)

  5. #5
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: delete text from cell after a certain word

    Marcol, as far as I know, the space doesn't matter, as the wildcard takes care of it

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: delete text from cell after a certain word

    @ arthurbr
    The point I was making might be illustrated by this string.

    "Windows Issue 2 SurfControl Web Filter Version 5.5 Service Pack 2 Service is Up"

    Should it return
    "Windows "
    or
    "Windows Issue 2 SurfControl Web Filter Version 5.5 Service Pack 2 Service "

    Perhaps that will never occur but similar issues might arise.

    Probably
    Find what: is * (space "is" space "*")

    as Don suggests is even more precise
    Last edited by Marcol; 01-21-2011 at 09:23 AM.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: delete text from cell after a certain word

    edit: below composed prior to above edit - relates to the "is *" example:

    Even then that approach is not watertight, for example:

    "This is great isn't it?"
    The Evaluate route allows OP to append string such that we can reduce risk of false positives

    " This is great isn't it? is "
    And fire a LEFT against it based on " is "

    Though searching for " is *" would resolve the above the Find/Replace would fail should "is" appear as either last or first word in the sentence whereas the Evaluate would succeed*

    *assuming you removed case sensitivity via an outer LOWER call - missing in my example!

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: delete text from cell after a certain word

    slightly more robust Evaluate below - added a TRIM & LOWER call

    .Value = Evaluate("IF(ISTEXT(" & x & "),TRIM(LEFT(" & x & ",FIND("" is "","" ""&LOWER(" & x & ")&"" is "")-1)),REPT(" & x & ",1))")
    I suspect however that in this instance the Find/Replace will work just fine!

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: delete text from cell after a certain word

    Agreed Don, you posted as I was editing to clarify the point.

  10. #10
    Registered User
    Join Date
    12-20-2010
    Location
    Southampton, England
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: delete text from cell after a certain word

    thanks guys. interesting debate going on! love it.......

+ Reply to Thread

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.6.0 RC 1