VBA: Add new line after a specific number of chatacters

BKChedlia

New Member
Joined
Jun 15, 2016
Messages
41
Hi,

I have a long texts in a specific column "B",

For each row, I need to split the text in multiple line (inside the same row: ALT + ENTER) after a fixed number of characters.

it's like when I wrap text

I need to use VBA si I found the code below in the web but it's not working

Help me please.

Code:
[/FONT][/COLOR]Sub[FONT=inherit] Sample()[/FONT] [COLOR=blue][FONT=inherit]Dim[/FONT][/COLOR] doc [COLOR=blue][FONT=inherit]As[/FONT][/COLOR] Document
 [COLOR=blue][FONT=inherit]Set[/FONT][/COLOR] doc = ActiveDocument
 
 [COLOR=blue][FONT=inherit]For[/FONT][/COLOR] i = doc.Range.Characters.[COLOR=blue][FONT=inherit]Count[/FONT][/COLOR] [COLOR=blue][FONT=inherit]To[/FONT][/COLOR] 1 [COLOR=blue][FONT=inherit]Step[/FONT][/COLOR] -1
 [COLOR=blue][FONT=inherit]If[/FONT][/COLOR] i [COLOR=blue][FONT=inherit]Mod[/FONT][/COLOR] 80 = 0 [COLOR=blue][FONT=inherit]Then[/FONT][/COLOR]
 doc.Range.Characters(i) = doc.Range.Characters(i) & vbNewLine
 [COLOR=blue][FONT=inherit]End[/FONT][/COLOR] [COLOR=blue][FONT=inherit]If[/FONT][/COLOR]
 [COLOR=blue][FONT=inherit]Next[/FONT][/COLOR]
[COLOR=blue][FONT=inherit]End[/FONT][/COLOR] [COLOR=blue][FONT=inherit]Sub
[/FONT][/COLOR][COLOR=blue][FONT=inherit]
[/FONT][/COLOR]
[COLOR=blue][FONT=inherit]



 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Activesheet.range("B:B").select

With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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