+ Reply to Thread
Results 1 to 5 of 5

Cutting off a string at the last occurrence of a character

Hybrid View

  1. #1
    Registered User
    Join Date
    07-24-2013
    Location
    Bratislava, SK
    MS-Off Ver
    Office Pro Plus 2016
    Posts
    42

    Cutting off a string at the last occurrence of a character

    Hello,

    I've been having trouble working out a function I need, and search didn't turn out anything.

    I have an Excel file in which cell "C10" lists a variable length string which contains a certain character ("-"), after which I need to discard characters I do not need. For example: supposing I have the string ThisString-1234567, I need to keep "ThisString" and discard "-1234567"

    I use the following formula:
    =IF(ISERROR(LEFT(C10,FIND("-",C10,3)-1)),"",LEFT(C10,FIND("-",C10,3)-1))

    The problem is, that this works only if there is only one "-" in the string, but sometimes, I have "This-String-or-Another-1234567", and get left with "This", whereas I need to keep the whole string, dashes and all, EXCEPT for the last one and what comes after (i.e. I need "This-String-or-Another").

    Could someone help me out?

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Cutting off a string at the last occurrence of a character

    =left(c10,find("^",substitute(c10,"-","^",len(c10)-len(substitute(c10,"-",""))))-1)
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    07-24-2013
    Location
    Bratislava, SK
    MS-Off Ver
    Office Pro Plus 2016
    Posts
    42

    Re: Cutting off a string at the last occurrence of a character

    Quote Originally Posted by Andy Pope View Post
    =left(c10,find("^",substitute(c10,"-","^",len(c10)-len(substitute(c10,"-",""))))-1)
    Andy - Works like a charm

    Thank you!

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Cutting off a string at the last occurrence of a character

    Try this also,

    =IF(ISERROR(FIND("-",C10)),"",LEFT(C10,FIND("-",C10,3)-1))

  5. #5
    Valued Forum Contributor
    Join Date
    09-15-2011
    Location
    Chennai, India
    MS-Off Ver
    Excel 2010
    Posts
    436

    Re: Cutting off a string at the last occurrence of a character

    You can this as well.

    =TRIM(SUBSTITUTE(LEFT(SUBSTITUTE(C10,"-",REPT("!",LEN(C10))),LEN(C10)),"!",""))
    Please click 'Add reputation', if my answer helped you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 7
    Last Post: 08-31-2013, 12:30 PM
  2. VBA Code for cutting character lenghts into another cell
    By ziemann82 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2013, 08:54 AM
  3. How to Delete Text Before First Occurrence Of Character?
    By JimMW in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-09-2012, 11:06 AM
  4. Replies: 4
    Last Post: 02-14-2006, 10:30 PM
  5. [SOLVED] Count occurrence of character within a cell
    By Kelli in forum Excel General
    Replies: 2
    Last Post: 01-18-2006, 10:25 AM

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