Split Sentence Into 40 Character Whole Words

roccoau

New Member
Joined
Dec 25, 2016
Messages
22
Office Version
  1. 365
Hi
I found below code on another site that does most of what I need it to do except for a few things I would like to add if possible.
The code splits all sentence's in column A into maximum character length of 40 whole words in each sentence.
Currently the split 40 character chunks are positioned in the columns next to the original sentence. (column B, C, D etc)
What I would like to achieve if possible is for a new row(s) added below original line(s) and the 40 character chunks added in these blank rows for all sentences in column A

Is this possible ?
Any help would be much appreciated
Tks



Sub breakTextAt40()

'' Cycles through all rows in column A putting a pipe every 40 characters without breaking whole words
For i = 1 To Range("a" & Rows.Count).End(xlUp).Row 'Sets the range to cycle through
Cells(i, 1).Activate 'Selects the cell to be split. i is the row, 1 is the column
Dim str_Out As String 'Variable to hold the new text string as it is created
Dim iloop As Integer 'Used as a counter for how many words are in the current string
Dim strString As Variant 'The original string will be split into an array and placed in this holder
Dim num As Integer 'Holds the max number of characters allowed
str_Out = "" 'Set empty value to put the new text in
num = 40 'Set the max number of characters. This number will increase each time it adds a new delimiter
strString = Split(ActiveCell.Value, " ") 'Splits the text into an array
For iloop = LBound(strString) To UBound(strString) 'Sets the number of cycles that the For Loop runs based on how many elements(words) are in the array
If iloop < UBound(strString) Then 'If the count of iloop is less then the max number of words, then keep running this loop
str_Out = str_Out & strString(iloop) & " " 'Takes the current string of text, adds the next word in the array, and a Space to separate it from the next word
If (Len(str_Out) + Len(strString(iloop + 1))) > num Then
str_Out = str_Out & "|" 'If the length of the current string plus the length of the next word of the string is greater then the text limit, then don't add the next word and add a pipe instead
num = Len(str_Out) + 40 'Count the current length of the text and add 40 to it
End If
End If
Next
str_Out = Trim(str_Out) 'Trim any extra whitespace off the text string
ActiveCell.Value = str_Out 'output the edited text string into the cell that the original text was in
Next



'' Split Column A with Text to Column using Piping as delimiter
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True


End Sub
 
If you are going to use a formula, give this a try. Leave cell A2 empty. Enter the formula in A3 with Ctrl + Shift + Enter, not just Enter.
Excel Workbook
AB
1The awards ceremony is first broadcast to radio in 1930 and televised in 1953. It is now seen live in more than 200 countries and can be streamed live online.[3] The Oscars is the oldest entertainment awards ceremony; its equivalents, the Emmy Awards for television, the Tony Awards for theatre, and the Grammy Awards for music and recording, are modeled after Academy Awards.376
2*0
3The awards ceremony is first broadcast38
4to radio in 1930 and televised in 1953.39
5It is now seen live in more than 20036
6countries and can be streamed live34
7online.[3] The Oscars is the oldest35
8entertainment awards ceremony; its34
9equivalents, the Emmy Awards for32
10television, the Tony Awards for theatre,40
11and the Grammy Awards for music and35
12recording, are modeled after Academy36
13Awards.7
Sheet

Istvan Hirsch

I'm trying to replicate your formula (as we are not allowed macros at work currently) but I cannot get it to work.

I've not done this previously - you say Ctrl+Shift+Enter.
I've entered tect into A1 then
I'm copying & pasting the whole line
{=IFERROR(LEFT(REPLACE(" "&A$1,1,SUMPRODUCT(LEN(A$2:A2)+1),""),MATCH(1,0/SEARCH(" ",MID(REPLACE(A$1&" ",1,SUMPRODUCT(LEN(A$2:A2)+1),""),ROW($1:$40),1)))),"")}
into A3 and then hit Ctrl+Shift+Enter but it doesn't return anything.

After hitting Ctrl+Shift+Enter nothing happens.
I'm still on cell A3 & the formula remains showing on the screen.

Any tips please?
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Select cell A1, then put the formula into the Formula Bar and do NOT hit the Enter Key, rather, press CTRL+SHIFT+ENTER instead. If you do it correctly, Excel will place curly braces around your formula (you cannot type them in yourself as that would not work). Once you have done this successfully, then you can copy/paste the formula into the other cells.

EDIT NOTE: I note from your post that the formula you put into the cell had curly braces around... you must put the formula into the cell WITHOUT those curly braces (see above for how they get placed around the formula).
 
Upvote 0
Rick
Thanks heaps - I got it sorted.
Was trying to paste it onto the cell instead of clicking IN THE FORMULA BAR and pasting the formula there & then CTRL+SHIFT+ENTER.

Thanks again.
Linsay
 
Upvote 0

Forum statistics

Threads
1,221,493
Messages
6,160,139
Members
451,624
Latest member
TheWes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top