+ Reply to Thread
Results 1 to 11 of 11

Split long text with max 30 characters keeping words intact to multiple rows using vb

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Split long text with max 30 characters keeping words intact to multiple rows using vb

    Hi

    I have a textbox on a userform where a long sentence can be inputted. This text then needs to be inserted in a column on a spreadsheet page that is a maximum of 30 characters wide. I need to develop a macro that will split the text, keeping words intact, to be inserted into a number of consecutive rows in a column. As an example, using the sentences in this post, it would look like this:

    I have a textbox on a userform
    where a long sentence can be
    inputted. This text then
    needs to be inserted in a
    column on a spreadsheet page
    that is a maximum of 30
    characters wide. I need to
    develop a macro that will
    split the text, keeping words
    intact, to be inserted into a
    number of consecutive rows in
    a column. As an example,
    using the sentences in this
    post, it would look like this:

    Any thoughts?

    Cheers

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    try something like this
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Enter the following code wherever you are firing off your macro and let me know if it helps.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Please Login or Register  to view this content.
    Last edited by mikerickson; 09-10-2012 at 01:35 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

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

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    @Mike: Think you're missing a Len function.

    @Nilem: Short and sweet.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Thanks all of you!

    BigBas, your code did split the long sentences to the required max, but unfortunately split some words as well. I was looking to keep the words intact.

    Mike, your code split the sentence into individual words. I will be able to use both your codes for other situation...however,

    Nilem...I just don't understand your code, but it did exactly what I wanted!!!!! Could I trouble you to adapt the code to split to a max of, say 46 characters? Could I wait for your response before I mark this thread as SOLVED?

    Thanks to all again.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Yes, I was missing a Len. I also failed to account for the user entering a line feed in the TextBox

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Wow, what a fantastic group of individuals in this Forum!!

    Mike, your code now works a treat. I am relatively a beginner in vb (newborn compared to you guys!), so would very much appreciate a line by line explanation of your code (if you have the time or inclination).

    Nilem's code also works but is a complete mystery (for me, anyway) to understand how it can be adapted for different parameters (eg different max lengths).

    Anyway, I am going to mark this thread as solved.

    Cheers to all.

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

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    but is a complete mystery (for me, anyway) to understand how it can be adapted for different parameters (eg different max lengths).
    It splits the string according to the width of the column, with consideration for font face and font size in each cell. You can't specify the length.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Dim oneWord as Variant, oneParen as Variant
    Dim oneLine as String

    Please Login or Register  to view this content.
    Last edited by mikerickson; 09-10-2012 at 03:39 PM.

  11. #11
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Split long text with max 30 characters keeping words intact to multiple rows using vb

    Thanks for taking the time to explain your code, Mike. It is very useful to know this so I can use it for different circumstances.

    shg, thanks also for explaining Nilem's code. I realised that by chance when I increased the column width, but good to get confirmation.

+ 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