+ Reply to Thread
Results 1 to 8 of 8

Condense text found in a cell

  1. #1
    Registered User
    Join Date
    03-12-2008
    Location
    Melbourne
    MS-Off Ver
    MS Excel 2016, 2021 & WPS(2023)
    Posts
    46

    Condense text found in a cell

    Hi All,

    I've got a fairly simple problem, |I have a string of text separated by forward slashes "/" with about 5 different things (e.g. fathead-sports/college/cfb/Michigan Wolverines)

    I want to have only the last two items listed in a new cell. (e.g. cfb/Michigan Wolverines)

    Source Sheet: RawDate
    Source Cell: U2

    Result Sheet: Result
    Result Cell: C2

    Help please?
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Condense text found in a cell

    I've added the following user defined function to your workbook:

    Please Login or Register  to view this content.

    To use it add the following to your target cell:

    =extractstring(Source cell) (for example =extractstring(RawData!U2))
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    07-05-2006
    Location
    Canada
    MS-Off Ver
    2003, 2007
    Posts
    581

    Re: Condense text found in a cell

    Hi,

    The macro solution provided will work fine. The only thing to be aware of is it may not work properly if there are fewer than two /'s in the string, or if there are only two /'s and the first one is the very first character.

    If you just want a pure formula method, here are a couple possibilities, which depend on how many /'s your data can have:

    First, I assume that the tilde (~) character will never appear in this cell. If it does, you would need to use something else that doesn't appear and substitute it for the ~ in the below formulas.

    1. If you are only ever going to have 3 /'s in your cell, the following will extract only the last bit:


    PHP Code: 
    =RIGHT(U2,LEN(U2)-FIND("~",SUBSTITUTE(U2,"/","~",2))) 

    2. If you could have any number of /'s (as long as there are two or more), the following will only extract the last bit:

    PHP Code: 
    =RIGHT(U2,LEN(U2)-FIND("~",SUBSTITUTE(U2,"/","~",LEN(U2)-LEN(SUBSTITUTE(U2,"/",""))-1))) 

    3. If you can have less than two /'s (ie. 1 or zero), you would need to encompass the above statement in an IF statement.

    PHP Code: 
    =IF(LEN(U2)-LEN(SUBSTITUTE(U2,"/",""))<2,U2,RIGHT(U2,LEN(U2)-FIND("~",SUBSTITUTE(U2,"/","~",LEN(U2)-LEN(SUBSTITUTE(U2,"/",""))-1)))) 

    Hope that helps,
    S
    Last edited by Maistrye; 11-28-2010 at 12:32 AM. Reason: Slight layout change

  4. #4
    Registered User
    Join Date
    09-14-2010
    Location
    Lynnwood, WA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Condense text found in a cell

    Quote Originally Posted by TheHobbit81 View Post
    Hi All,

    I've got a fairly simple problem, |I have a string of text separated by forward slashes "/" with about 5 different things (e.g. fathead-sports/college/cfb/Michigan Wolverines)

    I want to have only the last two items listed in a new cell. (e.g. cfb/Michigan Wolverines)

    Source Sheet: RawDate
    Source Cell: U2

    Result Sheet: Result
    Result Cell: C2

    Help please?
    Here you go:

    Option Explicit

    Sub ParseCell()
    Dim SplitText As Variant
    Dim i As Integer
    Dim s As String

    SplitText = Split(Sheets("RawData").Cells(2, 21), "/")
    i = UBound(SplitText)
    s = SplitText(i - 1) & "/" & SplitText(i)
    Sheets("Result").Cells(2, 3) = s
    End Sub

    thanks,

    Larry

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Condense text found in a cell

    moorelgm,

    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    03-12-2008
    Location
    Melbourne
    MS-Off Ver
    MS Excel 2016, 2021 & WPS(2023)
    Posts
    46

    Re: Condense text found in a cell

    Thanks to both Mojo2498 and Maistrye for your replies.

    I'm not very comfortable/ familiar with macros so I'm going to use Mojo's formula based answer.

    Works great, used No. 2. Just had to change it to: RawData!U2 and it all worked fine.

    Thanks all.

  7. #7
    Registered User
    Join Date
    03-12-2008
    Location
    Melbourne
    MS-Off Ver
    MS Excel 2016, 2021 & WPS(2023)
    Posts
    46

    Re: Condense text found in a cell

    Quote Originally Posted by TheHobbit81 View Post
    Thanks to both Mojo2498 and Maistrye for your replies.

    I'm not very comfortable/ familiar with macros so I'm going to use Mojo's formula based answer.

    Works great, used No. 2. Just had to change it to: RawData!U2 and it all worked fine.

    Thanks all.
    One last thing before I mark this as 'solved', what if I just wanted the last word? Sorry to be a pest.

  8. #8
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Condense text found in a cell

    =trim(right(substitute(RawData!U2," ",rept(" ",50)),25))
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ 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