counting characters to 50 and adding to new line

Bill Blackburn

New Member
Joined
Jul 6, 2009
Messages
5
Hi

Hoping someone can help. I need to be able to paste information into excel and then get excel to read the characters to 50 and then move the other info to another line and do the same command again until it has read all the data and put it in lines of 50.
 
Hello Mr. Beucaire, Your 2nd solution works well for an application I am working on. Can you additional functionality such that it does not cut into words. If the 70 count lands between O & O in the word WOOD, the macro would slice the string before WOOD. Mr. Beucaire I thank u very much for your time.
Best regards,
Jaime
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Here's a couple new versions that do not cut into words:

1) A1 is the source, pastes into the cells below it:

Code:
Sub Split30()
Dim MyVal As String, i As Long, MyArr As Variant, buf As String
MyArr = Split(Range("A1"), " ")

For i = LBound(MyArr) To UBound(MyArr)
    If Len(buf & " " & MyArr(i)) > 30 Then
        Range("A" & Rows.Count).End(xlUp).Offset(1).Value = WorksheetFunction.Trim(buf)
        buf = ""
    End If
    buf = buf & " " & MyArr(i)
    If i = UBound(MyArr) Then Range("A" & Rows.Count).End(xlUp).Offset(1).Value = WorksheetFunction.Trim(buf)
Next i

End Sub



2) The ACTIVECELL is the source, and it inserts blank rows below it so as to not overwrite anything that exists below the activecell:
Code:
Sub Split30()
Dim MyVal As String, i As Long, MyArr As Variant, buf As String
MyArr = Split(ActiveCell, " ")

    For i = LBound(MyArr) To UBound(MyArr)
        If Len(buf & " " & MyArr(i)) > 30 Then
            ActiveCell.Offset(1).Select
            ActiveCell.EntireRow.Insert xlShiftDown
            ActiveCell.Value = WorksheetFunction.Trim(buf)
            buf = ""
        End If
        buf = buf & " " & MyArr(i)
    Next i
    ActiveCell.Offset(1).Select
    ActiveCell.EntireRow.Insert xlShiftDown
    ActiveCell.Value = WorksheetFunction.Trim(buf)

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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