+ Reply to Thread
Results 1 to 7 of 7

Dividing a letters and numbers string

  1. #1
    Registered User
    Join Date
    01-25-2009
    Location
    Poland
    MS-Off Ver
    Excel 2000
    Posts
    4

    Dividing a letters and numbers string

    Hello,

    Recently I've stumbled upon a small problem, which I just cannot overcome.

    A given string of some letters (from three to five, six) is followed by a single number (1-4). Then it ends, or is followed by another such combination, up to a maximum of, let's say, four.

    Example:
    xxxx1
    yyy3yyyy1
    xyzxx1yxz4xzx1
    xxx2xxx2xxx2xxx2

    What I would like to do is to divide a given chain of characters into the smallest chains which contain only letters and one number. To the examples given above, I'd like to receive the following sets:
    xxxx1
    yyy3 and yyyy1
    xyzxx1 and yxz4 and xzx1
    ... and so on

    Is it somehow possible to do it with simple functions? Or is VB necessary (which sadly I don't know)?

    Thanks,
    Konkursor
    Last edited by konkursor; 01-25-2009 at 05:36 PM.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Here's some VBA that will do it.

    Please Login or Register  to view this content.
    Martin

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Here's a UDF you could use:
    Please Login or Register  to view this content.
    ... and an example of its usage:
    Please Login or Register  to view this content.
    To add it to the workbook:
    1. Copy the code from the post
    2. Press Alt+F11 to open the Visual Basic Editor (VBE)
    3. From the menu bar in the VBE window, do Insert > Module
    4. Paste the code in the window that opens
    5. Close the VBE to return to Excel

    It requires an array formula. So for the example above, select cells A1:F1, and enter the formula in the Formula Bar without the curly braces. Then hold down the Ctrl and Shift keys, then press Enter. The curly braces will appear. Then you can drag down the fill handle to copy the formula down.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    01-25-2009
    Location
    Poland
    MS-Off Ver
    Excel 2000
    Posts
    4
    Both of your formulas are working great, thank you!

    Just one more question, as apparently it turned out that there are some exceptions in the number strings. Sometimes, in the last part of the chain (or first being at the same time the last, if there are no numbers whatsoever) there are no numbers.
    For example with xyz4zyx is it possible to make it print out xyz4 and zyx1 (zyx from the end plus 1 added by the code). Maybe by somehow ordering it to print out everything what is left with an additional 1 after all the other cells have been done?

    Really sorry for the trouble, as I didn't notice it before...

  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
    Please Login or Register  to view this content.

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Here's a VBA version which copes with no numbers at the end.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-25-2009
    Location
    Poland
    MS-Off Ver
    Excel 2000
    Posts
    4
    Thank you very much, I really appreciate it!

+ 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