Customized text wrapping to new row (not within cell)

Extivalis

New Member
Joined
Mar 21, 2024
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I know about the default text wrapping option, but without getting too in the weeds (as I'm dealing with inherited workbooks, macros, processes, etc), is there a way to make text in one column cut off once it reaches the beginning (or end, either way) or a specified column, but then continue in a new row? for example:

Turn
| C | D | E | F | G |H| I |
3] WOW LOOK AT THIS TEXT AND HOW IT JUST KEEPS GOING ON AND ON
4]
5]

Into
| C | D | E | F | G |H| I |
3] WOW LOOK AT THIS TEXT AND HOW IT JUST
4] KEEPS GOING ON AND ON
5]

-I'm trying to add this into an existing macro
-Changing Column widths and Row heights isn't really an option (my hands are tied on the final formatting, there would be other data in A, B, and I)
-Doesn't matter if the cutoff is the right edge of Column G or Column H, but the text will be left justified and entered in Column C

Thanks in advance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Maybe this?

Book1.xlsb
CDEFGHIJ
1WOW LOOK AT THIS TEXT AND HOW IT JUST KEEPS GOING ON AND ON
2
3WOWLOOKATTHISTEXTANDHOW
4ITJUSTKEEPSGOINGONANDON
Sheet7
Cell Formulas
RangeFormula
D3:J4D3=WRAPROWS(TEXTSPLIT(C1," "),7,"")
Dynamic array formulas.
 
Upvote 0
For a cleaner solution, we should be measuring the cell text size and cutoff column using the win32 api but, we can use a hacky workaround by temporarly placing the code in a temporary textbox that is sized and formatted based on our requirements.

The following routine should do the job:
VBA Code:
Public Sub Split_Cell_In_Two_Rows( _
    ByVal SourceCell As Range, _
    ByVal CutOffColumn As String, _
    ByVal DestRow1 As Long, _
    ByVal DestRow2 As Long, _
    ByVal DestColumn As Long _
)
    If Len(SourceCell) Then
        With ActiveSheet.Shapes.AddTextbox( _
            msoTextOrientationHorizontal, _
            0&, _
            Cells(Rows.Count, 1&).Left, _
            Range(SourceCell, Columns(CutOffColumn)).Width, _
            0& _
            )
            With .TextFrame2.TextRange
                .Parent.MarginLeft = 0&
                .ParagraphFormat.LeftIndent = 0&
                .ParagraphFormat.RightIndent = 0&
                .Font.Name = SourceCell.Font.Name
                .Font.Size = SourceCell.Font.Size
                .Font.Bold = SourceCell.Font.Bold
                .Font.Italic = SourceCell.Font.Italic
                .Text = SourceCell.Text
                Cells(DestRow1, DestColumn) = .Lines(1&)
                Cells(DestRow2, DestColumn) = Replace(.Text, .Lines(1&), "")
            End With
            .Delete
        End With
    End If
End Sub

Here is an example of how to use the routine above:
This will split the text in C1 @ Column F and place the result in cells C1 and C2

VBA Code:
Sub Test()

    Split_Cell_In_Two_Rows _
            SourceCell:=Range("C1"), _
            CutOffColumn:="F", _
            DestRow1:=1, _
            DestRow2:=2, _
            DestColumn:=3
         
End Sub

1o.png



2o.png



Ideally, this routine should be tweaked to become more generic so that it works for splitting a cell into more than two rows.
 
Last edited:
Upvote 0
A VBA approach. This assumes that cells with text that might need to be split to multiple rows are in column A.
VBA Code:
Sub SplitLongTextExample()
    Dim WS As Worksheet, WS2 As Worksheet
    Dim rng As Range, rng2 As Range, R As Range
    Dim I As Long, MaxLen As Long
    Dim S As String, Line As String
    
    Application.ScreenUpdating = False
    
    Set WS = ActiveSheet
    Set WS2 = ThisWorkbook.Worksheets.Add  'temp sheet
    Set rng2 = WS2.Range("A1")
    
    With WS
        Set rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
    End With
    
    MaxLen = 40 '40 chars - this is an arbitrary value
    I = 1
    For Each R In rng
        If Len(R.Value) > MaxLen Then 'split into differnt rows
            Line = R.Value
            Do While Len(Line) > MaxLen
                S = RTrim(Left(Left(Line, MaxLen), InStrRev(Left(Line, MaxLen), " ")))
                rng2.Cells(I, 1).Value = Trim(S)
                I = I + 1
                Line = Mid(Line, Len(S) + 1, Len(Line))
            Loop
            If Trim(Line) <> "" Then
                rng2.Cells(I, 1).Value = Trim(Line)
            End If
        Else
            rng2.Cells(I, 1).Value = R.Value 'no need to split
        End If
        I = I + 1
    Next R
    
    If rng.Rows.Count + 1 < I Then
        WS2.UsedRange.Copy WS.Range("A2")
    End If
    
    Application.DisplayAlerts = False
    WS2.Delete 'temp sheet
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
For a cleaner solution, we should be measuring the cell text size and cutoff column using the win32 api but, we can use a hacky workaround by temporarly placing the code in a temporary textbox that is sized and formatted based on our requirements.

The following routine should do the job:
VBA Code:
Public Sub Split_Cell_In_Two_Rows( _
    ByVal SourceCell As Range, _
    ByVal CutOffColumn As String, _
    ByVal DestRow1 As Long, _
    ByVal DestRow2 As Long, _
    ByVal DestColumn As Long _
)
    If Len(SourceCell) Then
        With ActiveSheet.Shapes.AddTextbox( _
            msoTextOrientationHorizontal, _
            0&, _
            Cells(Rows.Count, 1&).Left, _
            Range(SourceCell, Columns(CutOffColumn)).Width, _
            0& _
            )
            With .TextFrame2.TextRange
                .Parent.MarginLeft = 0&
                .ParagraphFormat.LeftIndent = 0&
                .ParagraphFormat.RightIndent = 0&
                .Font.Name = SourceCell.Font.Name
                .Font.Size = SourceCell.Font.Size
                .Font.Bold = SourceCell.Font.Bold
                .Font.Italic = SourceCell.Font.Italic
                .Text = SourceCell.Text
                Cells(DestRow1, DestColumn) = .Lines(1&)
                Cells(DestRow2, DestColumn) = Replace(.Text, .Lines(1&), "")
            End With
            .Delete
        End With
    End If
End Sub

Here is an example of how to use the routine above:
This will split the text in C1 @ Column F and place the result in cells C1 and C2

VBA Code:
Sub Test()

    Split_Cell_In_Two_Rows _
            SourceCell:=Range("C1"), _
            CutOffColumn:="F", _
            DestRow1:=1, _
            DestRow2:=2, _
            DestColumn:=3
        
End Sub

View attachment 108815


View attachment 108816


Ideally, this routine should be tweaked to become more generic so that it works for splitting a cell into more than two rows.

This looks like what I'm shooting for, but I think I'm misunderstanding something on your example b/c my excel keeps giving the error "Compile error: Expected: expression", and highlighting the following in red:

SourceCell:=Range("C4"), _
CutOffColumn:="G", _
DestRow1:=4, _
DestRow2:=5, _
DestColumn:=3
 
Upvote 0
For a cleaner solution, we should be measuring the cell text size and cutoff column using the win32 api but, we can use a hacky workaround by temporarly placing the code in a temporary textbox that is sized and formatted based on our requirements.

The following routine should do the job:
VBA Code:
Public Sub Split_Cell_In_Two_Rows( _
    ByVal SourceCell As Range, _
    ByVal CutOffColumn As String, _
    ByVal DestRow1 As Long, _
    ByVal DestRow2 As Long, _
    ByVal DestColumn As Long _
)
    If Len(SourceCell) Then
        With ActiveSheet.Shapes.AddTextbox( _
            msoTextOrientationHorizontal, _
            0&, _
            Cells(Rows.Count, 1&).Left, _
            Range(SourceCell, Columns(CutOffColumn)).Width, _
            0& _
            )
            With .TextFrame2.TextRange
                .Parent.MarginLeft = 0&
                .ParagraphFormat.LeftIndent = 0&
                .ParagraphFormat.RightIndent = 0&
                .Font.Name = SourceCell.Font.Name
                .Font.Size = SourceCell.Font.Size
                .Font.Bold = SourceCell.Font.Bold
                .Font.Italic = SourceCell.Font.Italic
                .Text = SourceCell.Text
                Cells(DestRow1, DestColumn) = .Lines(1&)
                Cells(DestRow2, DestColumn) = Replace(.Text, .Lines(1&), "")
            End With
            .Delete
        End With
    End If
End Sub

Here is an example of how to use the routine above:
This will split the text in C1 @ Column F and place the result in cells C1 and C2

VBA Code:
Sub Test()

    Split_Cell_In_Two_Rows _
            SourceCell:=Range("C1"), _
            CutOffColumn:="F", _
            DestRow1:=1, _
            DestRow2:=2, _
            DestColumn:=3
      
End Sub

View attachment 108815


View attachment 108816


Ideally, this routine should be tweaked to become more generic so that it works for splitting a cell into more than two rows.
 
Upvote 0
I realize this may have something to do with other coding in the existing macros I inherited, but: it looks like when text is split into a second row, it's pushing all the other cells in the column down a row. Is it possible to not do that? For example, when the coding you suggested is run, there should be text in every 3rd row of column C, starting with C4 (so C4, C7, C10, etc, etc), and when the excess text from C4 is moved to C5, I would still need the text in C7 to start in C7 rather than getting bumped to C8. (luckily, there should never be so much text getting split that it would cause an overlap).
 
Upvote 0
A VBA approach. This assumes that cells with text that might need to be split to multiple rows are in column A.
VBA Code:
Sub SplitLongTextExample()
    Dim WS As Worksheet, WS2 As Worksheet
    Dim rng As Range, rng2 As Range, R As Range
    Dim I As Long, MaxLen As Long
    Dim S As String, Line As String
   
    Application.ScreenUpdating = False
   
    Set WS = ActiveSheet
    Set WS2 = ThisWorkbook.Worksheets.Add  'temp sheet
    Set rng2 = WS2.Range("A1")
   
    With WS
        Set rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
    End With
   
    MaxLen = 40 '40 chars - this is an arbitrary value
    I = 1
    For Each R In rng
        If Len(R.Value) > MaxLen Then 'split into differnt rows
            Line = R.Value
            Do While Len(Line) > MaxLen
                S = RTrim(Left(Left(Line, MaxLen), InStrRev(Left(Line, MaxLen), " ")))
                rng2.Cells(I, 1).Value = Trim(S)
                I = I + 1
                Line = Mid(Line, Len(S) + 1, Len(Line))
            Loop
            If Trim(Line) <> "" Then
                rng2.Cells(I, 1).Value = Trim(Line)
            End If
        Else
            rng2.Cells(I, 1).Value = R.Value 'no need to split
        End If
        I = I + 1
    Next R
   
    If rng.Rows.Count + 1 < I Then
        WS2.UsedRange.Copy WS.Range("A2")
    End If
   
    Application.DisplayAlerts = False
    WS2.Delete 'temp sheet
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub


I realize this may have something to do with other coding in the existing macros I inherited, but: it looks like when text is split into a second row, it's pushing all the other cells in the column down a row. Is it possible to not do that? For example, when the coding you suggested is run, there should be text in every 3rd row of column C, starting with C4 (so C4, C7, C10, etc, etc), and when the excess text from C4 is moved to C5, I would still need the text in C7 to start in C7 rather than getting bumped to C8. (luckily, there should never be so much text getting split that it would cause an overlap).
 
Upvote 0
For example, when the coding you suggested is run, there should be text in every 3rd row of column C, starting with C4 (so C4, C7, C10, etc, etc),
That is a very important detail to omit. One with a major impact on how any solution would be coded. so of course the code I posted won't do that. Please use the free addin XL2BB to post some sample data that shows both "before" data and the "after" result.

 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,718
Members
452,667
Latest member
vanessavalentino83

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