How do I separate a sentence in Microsoft Excel into smaller peaces?

Posted on 2011-05-04
Last Modified: 2012-05-11
I have a large  sentences in a column and i want separate them to smaller pieces that do not exceed a specific amount of characters. Worlds should not be affected.

For example lets say that I have the cells:

[A wiki enables communities to write documents collaboratively, using a simple markup language and a web browser.]
[There are many different ways in which wikis have users edit the content.]

I want to take:

[A wiki enables communities]
[to write documents collaboratively,]
[markup language and a web browser.]
[There are many different ways in which]
[wikis have users edit the content.]

These cells should not have more than 40 characters

Any Ideas?

Thank you very much.
Question by:panostso
    LVL 33

    Accepted Solution

    I've attached a sample file for you to test. It will split the text on cells from column  A and will put them on cells on column B with a maximum of 30 chars without breaking the words.

    Function SubStrings(ByVal sInp As String, nMax As Long) As String()
        Dim iOut        As Long
        Dim iBeg        As Long
        Dim iPosNew     As Long
        Dim iPosOld     As Long
        Dim sSep        As String
        sSep = Chr(143)    ' an unused character
        sInp = Replace(sInp, Chr(160), " ")    ' change non-breaking spaces to spaces
        sInp = WorksheetFunction.Trim(sInp)
        Do Until Len(sInp) - InStrRev(sInp, sSep) <= nMax
            iBeg = iPosOld + nMax + 1
            If iBeg > Len(sInp) Then iBeg = Len(sInp)
            iPosNew = InStrRev(sInp, " ", iBeg)
            If iPosNew <= iPosOld Then
                sInp = Left(sInp, iPosOld + nMax) & sSep & Mid(sInp, iPosOld + nMax + 1)
                iPosOld = iPosOld + nMax + 1
                Mid(sInp, iPosNew) = sSep
                iPosOld = iPosNew
            End If
        SubStrings = Split(sInp, sSep)
    End Function
    Sub SplitEm()
        Const MaxLen    As Long = 30
        Dim cell        As Range
        Dim astr()      As String
        For Each cell In Range("A1", Cells(Rows.Count, "A").End(xlUp))
            astr = SubStrings(cell.Value, MaxLen)
            Select Case UBound(astr)
                Case -1
                Case 0
                    cell.Offset(, 1).Value = astr(0)
                Case Else
                    cell.Offset(, 1).Resize(UBound(astr) + 1).Value = WorksheetFunction.Transpose(astr)
            End Select
        Next cell
    End Sub

    Open in new window


    Author Closing Comment

    Excellent! This is exactly what I need.
    Could you please provide some help on how i will insert this code to my sheet.

    Thanks again
    LVL 33

    Expert Comment

    Take a look at this article that explains how to put a macro on a sheet:

    And thanks for the grade... :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
    This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now