Splitting a string out in a cell into 60 character chunks

laundon

New Member
Joined
Jan 5, 2005
Messages
18
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello!

I have a problem that I would like help solving if I may. I have a spreadsheet that has a cell containing a string of text. I need to split this cell into 60 character chunks, preserving whole words if at all possible. The maximum cell length I have is 240 characters so I'll need to split my single cell into up to 4 separate cells of 60 characters.

Am I asking the impossible, or is this doable in Excel?

Any advice/help greatly appreciated!

Kind Regards,
Tim.
 
Hi
just replace
VBA Code:
Offset(x, 1)
with
VBA Code:
Offset(,x)
In two places
 
Upvote 0
Awesome. I am touched by your quick response. Thanks a ton.

However, it would be even great, if we could run it on multiple cells. I am sure, you must tried that too.

Thanks again.
 
Upvote 0
VBA Code:
Sub v()
Dim ray() As String, i%, ns$, x%
If Selection.Count > 1 Then
    MsgBox "select one cell only."
    Exit Sub
End If
ray = Split(WorksheetFunction.Trim(Selection), " ")
ns = ray(0)
x = 1
For i = 1 To UBound(ray)
    If Len(ns) + Len(ray(i)) + 1 < 60 Then
        ns = ns & " " & ray(i)
    Else
        Selection.Offset(0, x).Value = ns
        ns = ray(i)
        x = x + 1
    End If
Next
Selection.Offset(0, x) = ns
End Sub
This works amazingly well. Thank a ton for sharing. However, is there a way to run it on multiple cells simultaneously.
 
Upvote 0
Try
VBA Code:
Sub v()
    Dim ray() As String, i%, ii%, ns$, x%
    For ii = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        ray = Split(Trim(Cells(ii, 1)), " ")
        ns = ray(0)
        x = 1
        For i = 1 To UBound(ray)
            If Len(ns) + Len(ray(i)) + 1 < 60 Then
                ns = ns & " " & ray(i)
            Else
                Cells(ii, 1).Offset(, x).Value = ns
                ns = ray(i)
                x = x + 1
            End If
        Next
        Cells(ii, 1).Offset(, x) = ns
    Next
End Sub
 
Upvote 0
Try
VBA Code:
Sub v()
    Dim ray() As String, i%, ii%, ns$, x%
    For ii = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        ray = Split(Trim(Cells(ii, 1)), " ")
        ns = ray(0)
        x = 1
        For i = 1 To UBound(ray)
            If Len(ns) + Len(ray(i)) + 1 < 60 Then
                ns = ns & " " & ray(i)
            Else
                Cells(ii, 1).Offset(, x).Value = ns
                ns = ray(i)
                x = x + 1
            End If
        Next
        Cells(ii, 1).Offset(, x) = ns
    Next
End Sub
Subscript out of range error
for following
ns = ray(0)
 
Upvote 0
So you have empty row/s?
VBA Code:
Sub v()
    Dim ray() As String, i%, ii%, ns$, x%
    For ii = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        On Error Resume Next
        ray = Split(Trim(Cells(ii, 1)), " ")
        ns = ray(0)
        x = 1
        For i = 1 To UBound(ray)
            If Len(ns) + Len(ray(i)) + 1 < 60 Then
                ns = ns & " " & ray(i)
            Else
                Cells(ii, 1).Offset(, x).Value = ns
                ns = ray(i)
                x = x + 1
            End If
        Next
        Cells(ii, 1).Offset(, x) = ns
    Next
End Sub
 
Upvote 0
So you have empty row/s?
VBA Code:
Sub v()
    Dim ray() As String, i%, ii%, ns$, x%
    For ii = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        On Error Resume Next
        ray = Split(Trim(Cells(ii, 1)), " ")
        ns = ray(0)
        x = 1
        For i = 1 To UBound(ray)
            If Len(ns) + Len(ray(i)) + 1 < 60 Then
                ns = ns & " " & ray(i)
            Else
                Cells(ii, 1).Offset(, x).Value = ns
                ns = ray(i)
                x = x + 1
            End If
        Next
        Cells(ii, 1).Offset(, x) = ns
    Next
End Sub
I have empty rows only at the end of the the data, not in between.
 
Upvote 0
So you have empty row/s?
VBA Code:
Sub v()
    Dim ray() As String, i%, ii%, ns$, x%
    For ii = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        On Error Resume Next
        ray = Split(Trim(Cells(ii, 1)), " ")
        ns = ray(0)
        x = 1
        For i = 1 To UBound(ray)
            If Len(ns) + Len(ray(i)) + 1 < 60 Then
                ns = ns & " " & ray(i)
            Else
                Cells(ii, 1).Offset(, x).Value = ns
                ns = ray(i)
                x = x + 1
            End If
        Next
        Cells(ii, 1).Offset(, x) = ns
    Next
End Sub
This one works well. Thanks a lot.
 
Upvote 0

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