+ Reply to Thread
Results 1 to 14 of 14

Complex formula: Split text into several chunks based on character count

  1. #1
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Question Complex formula: Split text into several chunks based on character count

    Hi team,

    I have this formula that is meant to split the source text in cell E2 into several adjacent cells (F2, G2...) where no cell should exceed 200 characters. At the same time, I'm doing a basic clean up:

    Please Login or Register  to view this content.
    So far so good, it's working as expected, however, it is

    # splitting words across cells, e.g. "acc" in F2 and "ess" in G2. I would need to make sure that the words stay in tact and the breakpoint is essentially the last blank space before the 200th character.
    # it truncates the last text block and does not output the last few words

    Any idea how to solve this?

    Attaching an example workbook. Any help appreciated
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,215

    Re: Complex formula: Split text into several chunks based on character count

    VBA

    Please Login or Register  to view this content.
    Attached Files Attached Files
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: Complex formula: Split text into several chunks based on character count

    Formula. F2:
    =TRIM(TEXTBEFORE(LEFT(E2,200)," ",-1))

    G2, copied across:
    =IF(F2="","",TRIM(TEXTBEFORE(LEFT(TEXTAFTER($E2,F2),200)," ",-1)))
    Attached Files Attached Files
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Complex formula: Split text into several chunks based on character count

    Quote Originally Posted by Glenn Kennedy View Post
    Formula. F2:
    =TRIM(TEXTBEFORE(LEFT(E2,200)," ",-1))
    I think you should change the 200 to 201. That way, if the 200th character occurred immediately before a space character (in the 201st position), the entire 200 characters would be retained. As your formula is written now (with the 200), that last word would be "thrown away" unnecessarily. I did not try your second formula, but it looks like the same change should be made there as well.

  5. #5
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: Complex formula: Split text into several chunks based on character count

    Nice one Rick. Have some rep...


    Formula. F2:
    =TRIM(TEXTBEFORE(LEFT(E2,201)," ",-1))

    G2, copied across:
    =IF(F2="","",TRIM(TEXTBEFORE(LEFT(TEXTAFTER($E2,F2),201)," ",-1)))

    I was just pleased that it worked. I didn't test it extensively...
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,386

    Re: Complex formula: Split text into several chunks based on character count

    You miss the last word :p

    G2 has to be

    =IF(F2="","",TRIM(TEXTBEFORE(LEFT(TEXTAFTER($E2&" ",F2),201)," ",-1)))

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: Complex formula: Split text into several chunks based on character count

    And there was me, thinking "how clever am I??"

    Mehh. Back to the idiot's corner.

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Complex formula: Split text into several chunks based on character count

    Quote Originally Posted by JEC. View Post
    You miss the last word :p

    G2 has to be

    =IF(F2="","",TRIM(TEXTBEFORE(LEFT(TEXTAFTER($E2&" ",F2),201)," ",-1)))
    Actually, I do not think that optimizes the output. Using your formula revision, I get cell lengths as follows...

    198 191 197 192 192 193 192 195 112

    Using this formula that I developed for cell G2 (to be copied across)...

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I get the following cell lengths...

    198 191 197 200 194 200 191 199 83

    Notice that a couple of the cells fill out to the full 200. So I would be inclined to use Glenn's first formula and the formula above for the second formula.

    EDIT NOTE: That LET function call is not needed here (I had a different formula originally that required it); so, we can shorten the second formula (that should be placed in cell G2 and copied across) to this...
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 07-05-2023 at 11:47 AM.

  9. #9
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,737

    Re: Complex formula: Split text into several chunks based on character count

    I've not been following this in detail, but I thought the OP had said:

    ... it truncates the last text block and does not output the last few words ...
    so does that mean the final 83 characters (or 112 in Glenn's formula) should be omitted?

    Pete

  10. #10
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Complex formula: Split text into several chunks based on character count

    Awesome, thanks all! The formula works perfectly I'll also save the VBA, just in case...

    Thanks for the super quick help!

  11. #11
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Complex formula: Split text into several chunks based on character count

    Please try

    =LET(R,LAMBDA(R,a,b,LET(c,LEN(a)<201,d,IF(c,a,TEXTBEFORE(LEFT(a,201),{"."," "},-1,,1)),
    IF(c,DROP(HSTACK(b,d),,1),R(R,MID(a,LEN(d)+2,6^6),HSTACK(b,d))))),R(R,E2,""))
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,386

    Re: Complex formula: Split text into several chunks based on character count

    Here a recursive VBA method, same output as Rick's Formula

    Please Login or Register  to view this content.
    Last edited by JEC.; 07-05-2023 at 01:08 PM.

  13. #13
    Registered User
    Join Date
    12-01-2011
    Location
    India
    MS-Off Ver
    Microsoft 365
    Posts
    33

    Lightbulb Re: Complex formula: Split text into several chunks based on character count

    Hi

    As per my understanding we need to write a code to fulfill this requirement. There is an option with power query to separate the text into columns after 200 characters.
    However, based on your requirement of complete word at the end, I have written a simple code to get the results in sheet "Final Result".
    You can change the data in column 1 and run a macro to check the result in attached sheet. Let me know in case of any questions.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: Complex formula: Split text into several chunks based on character count

    Thanks all! Appreciate the many answers. Went with Glenn's solution in the end, but keeping the other in the back pocket if I run into any issues. Thank 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. [SOLVED] complex split formula to split column to different columns
    By JEAN1972 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-29-2023, 04:23 AM
  2. Formula to split text from each specific character to spill across range.
    By Mgc26133 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-28-2022, 12:23 PM
  3. [SOLVED] Semi complex count formula based on multiple criteria
    By the machine in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-09-2018, 11:14 AM
  4. Split An Array In Chunks
    By DD646 in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 10-13-2017, 06:40 AM
  5. Replies: 3
    Last Post: 10-21-2016, 07:46 PM
  6. String too long for VBA. Can it be split based on character count?
    By icyrius in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 07-08-2016, 01:41 PM
  7. [SOLVED] Split string based on character count
    By cedric_dranreb in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-19-2013, 05:14 AM

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