Hello everyone,
Im trying to derust my VBA skills and am pretty sure this macro is fully feasible to fix as I have been using similar in the past.
What I need:
- Im pasting unformatted text into column A,
- Each of the rows can contain a random amount of characters <1,+inf)
- The macro is supposed to split the text into max 35 character per row and then move the remaining text into the next row without losing any text
- I do not care about text being split in the middle of word
What I have built from memory (enhanced a bit by AI but it broke it even more to be honest):
Issues I am facing:
Out of
Which is a complete gibberish. Does any one have a clue on what is wrong here?
Thanks in advance.
Im trying to derust my VBA skills and am pretty sure this macro is fully feasible to fix as I have been using similar in the past.
What I need:
- Im pasting unformatted text into column A,
- Each of the rows can contain a random amount of characters <1,+inf)
- The macro is supposed to split the text into max 35 character per row and then move the remaining text into the next row without losing any text
- I do not care about text being split in the middle of word
What I have built from memory (enhanced a bit by AI but it broke it even more to be honest):
VBA Code:
Sub SplitTextIntoRows()
Dim ws As Worksheet
Dim cell As Range
Dim originalText As String
Dim newText As String
Dim rowNum As Long
Dim colNum As Long
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
' Loop through each cell in column A
For Each cell In ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
originalText = cell.Value
rowNum = cell.Row
colNum = cell.Column
' Clear the cell before adding new values
cell.ClearContents
' Split the text into rows of 35 characters each
Do While Len(originalText) > 0
' Check if the remaining text is less than or equal to 35 characters
If Len(originalText) <= 35 Then
' If the remaining text fits within the current cell, set the value and exit the loop
cell.Value = originalText
Exit Do
Else
' Split the text at 35 characters
newText = Left(originalText, 35)
originalText = Mid(originalText, 36)
' Move to the next cell in the column
If ws.Cells(rowNum + 1, colNum).Value <> "" Then
rowNum = rowNum + 1
ws.Cells(rowNum, colNum).EntireRow.Insert
Else
rowNum = rowNum + 1
End If
' Set the value in the next cell
ws.Cells(rowNum, colNum).Value = newText
End If
Loop
Next cell
End Sub
Issues I am facing:
Out of
I am getting
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit. In leo justo, scelerisque a consequat at, . 2 Sed efficitur accumsan ante, tincidunt tempor lacus accumsan nec . 3 Morbi ipsum . 4. Aliquam blandit fermentum lacus ut malesuada. Praesent pretium ex nec mi fringilla dignissim
o, scelerisque a consequat at, 1 Lorem ipsum dolor sit amet, conse ctetur adipiscing elit. In leo just . idunt tempor lacus accumsan nec 2 Sed efficitur accumsan ante, tinc . 3 Morbi ipsum . ec mi fringilla dignissim 4. Aliquam blandit fermentum lacus ut malesuada. Praesent pretium ex n
Which is a complete gibberish. Does any one have a clue on what is wrong here?
Thanks in advance.