+ Reply to Thread
Results 1 to 9 of 9

Split a line of words, each word in a separate cell using function

  1. #1
    Registered User
    Join Date
    10-09-2003
    Posts
    25

    Split a line of words, each word in a separate cell using function

    All,
    I am looking for something similar as described in http://www.excelforum.com/excel-gene...rate-cell.html except that I am looking to code a user defined function, "SplitText" with 1 argument. My text are delimited by | but with various parts and sometime empty part. The difference is that I don't need to have row 1 to have the number as in the previous thread. Please help. Thanks.

    Please Login or Register  to view this content.
    Last edited by slc; 08-08-2012 at 09:54 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Split a line of words, each word in a separate cell using function

    Maybe to can achieve it with text to column.

    Excel 2007 => data => text to column
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Registered User
    Join Date
    10-09-2003
    Posts
    25

    Re: Split a line of words, each word in a separate cell using function

    oeldere,

    Thanks. However, text to column did not work for me because the tool will remove all the original data in Column A and not all the data I have in Column A are to be split. Furthermore my Column B, C and so on contain other info which are overwritten when I use Text to column function. Hence, I am looking for a user defined function, which is more flexible.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: Split a line of words, each word in a separate cell using function

    could you copy the data to a new column away from your original data and try text-to-colums there and then just use what you need?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Registered User
    Join Date
    10-09-2003
    Posts
    25

    Re: Split a line of words, each word in a separate cell using function

    FDibbins,
    By doing so, I am doing selective copy and paste, which introduce human error. I will doing this splitting every weeks with different files.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Split a line of words, each word in a separate cell using function

    Here you go, put this function into a regular module (Insert Module), then use it as you've described as a formula function.
    The function actually has 3 parameters, not two. You can specify the delimiter as a third option:

    =SplitText(A1, 1)
    =SplitText(A1, 1, "|")

    If you leave out the third parameter, it defaults to the | character, but you can control it if you wish.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 08-08-2012 at 08:20 PM. Reason: Codelines were in the wrong order, fixed now
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  7. #7
    Registered User
    Join Date
    10-09-2003
    Posts
    25

    Re: Split a line of words, each word in a separate cell using function

    JBeaucaire,

    I am blown away on the elegance of the code and your thoughtfulness on the 2nd argument. It makes the UDF more generic. I did 1 minor change i.e. MyNum + 1 to handle the last part correctly. Thanks a zillion.

    Please Login or Register  to view this content.
    Btw, if you know how to mark this thread as [SOLVED], please let me know.
    Last edited by slc; 08-08-2012 at 09:50 PM.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Split a line of words, each word in a separate cell using function

    Changing the -1 to +1 breaks the function in my testing.

    In your original example strings, the delimiter is the first character as well as between. That's atypical, usually the first delimiter occurs between the first two strings.

    Also, when creating an array (textARR) in the function, they are zero-based arrays, so the first field is called 0, not 1.

    This works well in your atypical strings because the first character being a | means the first field in the array is empty, so your first string really is is in textARR(1), convenient.

    But I realized you might occasionally have strings that DON'T have the starting character, a more typical reality. In that case, to get the first string, you would want textARR(0), not (1). So, I check, and if the first character is NOT the delimiter, I subtract 1 from the number in your second parameter.

    So, your change should yield the 3rd string if the first character is missing.

    Anyway, please test all that carefully.

    See the THREAD TOOLS menu at the top of this thread.

  9. #9
    Registered User
    Join Date
    10-09-2003
    Posts
    25

    Re: Split a line of words, each word in a separate cell using function

    Jerry,

    Thanks. Really appreciate your effort in explaining. Will proceed with caution.

+ 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