[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-04
3
Medium Priority
?
300 Views
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.
0
Comment
Question by:panostso
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
jppinto earned 2000 total points
ID: 35689466
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.

jppinyo
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
        Else
            Mid(sInp, iPosNew) = sSep
            iPosOld = iPosNew
        End If
    Loop

    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)).EntireRow.Insert
                cell.Offset(, 1).Resize(UBound(astr) + 1).Value = WorksheetFunction.Transpose(astr)
        End Select
    Next cell
End Sub

Open in new window

Split-Sentences.xlsm
0
 

Author Closing Comment

by:panostso
ID: 35689629
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
0
 
LVL 33

Expert Comment

by:jppinto
ID: 35690590
Take a look at this article that explains how to put a macro on a sheet:

http://www.mrexcel.com/articles/paste-macro-into-vbe.php

And thanks for the grade... :)

jppinto
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

864 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