VBA Wrap Text after "x" amount of characters

Dannottheman

Board Regular
Joined
Dec 24, 2020
Messages
55
Office Version
  1. 2007
Hello,

I searched and did not find an answer to this here or other sites that would work.

I need a VBA that will resize the column width/wrap the text of all the rows in column A if they exceed "x" amount of characters. (Issue: I am converting the sheet as a PDF and if the text is too long then it appears outside the PDF page or as a separate page). The whole last word/text of the row should move onto the next row (perhaps this is called "orphaning"?. In other words, text should not get split in two, the whole last word should move down to the next sentence and then the rest of the text. If more than one sentence within the same row exceeds "x" characters, they should also wrap following the same rule. For example, if the limit were 10 characters:
Existing:
This is a test message
New (after wrap):
This is a test
message
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Why not reduce the columns width and enable text wrap?
 
Upvote 0
Yes, but I would need to guess if the last word will appear inside or outside the PDF page. Sometimes they appear outside the PDF, because some of the text are formulas (as opposed to regular words), it is not always easy to catch. I suppose if there was a VBA where I could set the width of the column to "x" pixels, it would work too. Then I can just click "text wrap" after the column is resized. Right now each worksheet has to go through about 10 different steps. My goal is to just use VBAs and copy paste the code or use one long compilation of VBAs instead of individual steps. I have approximately 235 of these worksheets and if I do each step manually, that's 2350 steps. I also need to print two different versions of each worksheet so even a small step will add up to many hours in the end.
 
Upvote 0
First you need to determine the column width that will allow the text always to fit in the PDF as intended.
I suppose VBA can't help with this first step.
After that, it's going to be possible to write a VBA code to set the column width and enable text wrap.
Note that the line height must be sufficient to accommodate the text as well, but in this case VBA autofit will help.
 
Last edited:
Upvote 0
This is a simple hypothetical example:
VBA Code:
With Sheets("Myworksheetname").columns("B")
   .ColumnWidth=10
   .WrapText=true
End with
This will set "Myworksheetname" column "B" to width 10 and enable text wrap.
I suppose you need to convert "707 px" to the excel column width system.
Test in a throw away copy of your file.
 
Upvote 0
This is a simple hypothetical example:
VBA Code:
With Sheets("Myworksheetname").columns("B")
   .ColumnWidth=10
   .WrapText=true
End with
This will set "Myworksheetname" column "B" to width 10 and enable text wrap.
I suppose you need to convert "707 px" to the excel column width system.
Test in a throw away copy of your file.
I am getting error "Compilation error" "The external process is not valid". By the way, I see that the worksheet has a name assigned in the VBA "Myworksheetname", is there a way to make it default to the active one being used? I have a couple of hundred of these and I would hate to have to edit the VBA each time with a new name.
 
Upvote 0
Replace Sheets("Myworksheetname") with Activesheet
Replace "B" with actual intended column.
And "10" with actual intended column width.
 
Upvote 0
Thanks. When I do this, it opens up a little box and it asks me to give the VBA a name. I give it a name and then click on "Execute" and get the following error: "Compilation error" "The external process is not valid".
 
Upvote 0
VBA Code:
Sub Test()
Application.screenupdating=false
With Activesheet.columns("B")
   .ColumnWidth=10
   .WrapText=true
End with
Application.screenupdating=true
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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