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]



 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Vinoth_Shob

New Member
Joined
Dec 11, 2017
Messages
30
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,314
Members
414,053
Latest member
Dual Showman

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
Top