VBA to add hard return in every cell in whole column

kit99

Active Member
Joined
Mar 17, 2015
Messages
352
I'm trying to make a pivot graph look nice for a Dashboard. X axis lables is far to long, so I've already run a vba that limits caracters to max 20.
But I also need a vba to add hard return to every cell in the whole column. Hard return should be added after caracter number 5 in all cells in column.
I'm using headers, so this should be done from M2 and to the end of active cells in column M.
Is this possible?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Been testing a workaround...
If I copy first 5 caracters + line break (Chr(10)) + copy caracters from left no 6 into a new column, this should work.
But there's something wrong with the "Right(Cell.Value, (Len(-6))"-part of my vba below.
Anyone that knows how I can make the vba below copy caracters from caracter no 6 and to the end?
I can't use "Right 15", as not all cells holds 20 caracters. Some holds less.

Code:
Dim Cell As Range, cRange As Range
LastRow = ActiveSheet.Cells(Rows.Count, "L").End(xlUp).Row
Set cRange = Range("L2:L" & LastRow)
    For Each Cell In cRange
            Cell.Offset(0, 1).Value = Left(Cell.Value, 5) & Chr(10) & [B]Right(Cell.Value, (Len(-6))[/B])
            
    Next Cell
 
Last edited:
Upvote 0
Solved it myself... This line works:

Code:
Cell.Offset(0, 1).Value = Left(Cell.Value, 5) & Chr(10) & Right(Cell.Value, (Len(Cell.Value) - 5))
 
Upvote 0
For the entire range
Code:
Sub t()
Dim c As Range
    With ActiveSheet
        For Each c In .Range("M2", .Cells(Rows.Count, "M").End(xlUp))
            c = Left(c.Value, 5) & vbCrLf & Right(c.Value, Len(c.Value) - 5)
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,024
Members
448,543
Latest member
MartinLarkin

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