Split Sentence Into 40 Character Whole Words

sduttexcel

New Member
Joined
Mar 16, 2018
Messages
22
I have created a macro to split a column to 40 characters. The split moves to next rows but I want split to move to columns. i.e. If cell A1 is 100 characters long. The split should be B1, C1 and so on. Any help appreciated.
 
I have highlighted in red where I get error.
LInesOfLength is the function you posted in Message #1 ... Fluff was just amending the macro part of your posting. That is why I prefaced my code by saying "Here is a fully self-contained macro (no side function calls required)" so that you would not think you needed to retain your LInesOfLength function as opposed to your need to keep it for the code Fluff posted.
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi Rick,

The macro works perfectly when the column is populated. I get the error message when there is a blank column. Say A1 to A5 are populated than A6 is blank than A7 is populated again and so on. Can this be fixed. The data file has no more than 10,000 records, if this will help. many thanks!

Sub SplitTextOnSpacesWithMaxCharactersPerLine() Dim Text As String, TextMax As String, SplitText As String, Answer() As String Dim Space As Long, Source As Range, CellWithText As Range Const MaxChars As Long = 40 Const DestinationOffset As Long = 1 Set Source = Range("A1", Cells(Rows.Count, "A").End(xlUp)) On Error GoTo 0 For Each CellWithText In Source Text = CellWithText.Value SplitText = "" Do While Len(Text) > MaxChars TextMax = Left(Text, MaxChars + 1) If Right(TextMax, 1) = " " Then SplitText = SplitText & RTrim(TextMax) & vbLf Text = Mid(Text, MaxChars + 2) Else Space = InStrRev(TextMax, " ") If Space = 0 Then SplitText = SplitText & Left(Text, MaxChars) & vbLf Text = Mid(Text, MaxChars + 1) Else SplitText = SplitText & Left(TextMax, Space - 1) & vbLf Text = Mid(Text, Space + 1) End If End If Loop Answer = Split(SplitText & Text, vbLf) CellWithText.Offset(, DestinationOffset).Resize(, UBound(Answer) + 1).Value = Answer Next Exit SubEnd Sub
 
Upvote 0
Hi Rick,

The macro works perfectly when the column is populated. I get the error message when there is a blank column. Say A1 to A5 are populated than A6 is blank than A7 is populated again and so on. Can this be fixed. The data file has no more than 10,000 records, if this will help. many thanks!
Give this code a try instead then...
Code:
[table="width: 500"]
[tr]
	[td]Sub SplitTextOnSpacesWithMaxCharactersPerLine()
  Dim Text As String, TextMax As String, SplitText As String, Answer() As String
  Dim Space As Long, Source As Range, CellWithText As Range

  Const MaxChars As Long = 40
  Const DestinationOffset As Long = 1

  Set Source = Range("A1", Cells(Rows.Count, "A").End(xlUp))
  On Error GoTo 0
  For Each CellWithText In Source
    If Len(CellWithText) Then
      Text = CellWithText.Value
      SplitText = ""
      Do While Len(Text) > MaxChars
        TextMax = Left(Text, MaxChars + 1)
        If Right(TextMax, 1) = " " Then
          SplitText = SplitText & RTrim(TextMax) & vbLf
          Text = Mid(Text, MaxChars + 2)
        Else
          Space = InStrRev(TextMax, " ")
          If Space = 0 Then
            SplitText = SplitText & Left(Text, MaxChars) & vbLf
            Text = Mid(Text, MaxChars + 1)
          Else
            SplitText = SplitText & Left(TextMax, Space - 1) & vbLf
            Text = Mid(Text, Space + 1)
          End If
        End If
      Loop
      Answer = Split(SplitText & Text, vbLf)
      CellWithText.Offset(, DestinationOffset).Resize(, UBound(Answer) + 1).Value = Answer
    End If
  Next
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,815
Messages
6,121,715
Members
449,049
Latest member
THMarana

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