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
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
693
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Why not reduce the columns width and enable text wrap?
 

Dannottheman

Board Regular
Joined
Dec 24, 2020
Messages
55
Office Version
  1. 2007
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.
 

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
693
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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:

Dannottheman

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

ADVERTISEMENT

It's 707 px
 

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
693
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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.
 

Dannottheman

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

ADVERTISEMENT

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.
 

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
693
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Replace Sheets("Myworksheetname") with Activesheet
Replace "B" with actual intended column.
And "10" with actual intended column width.
 

Dannottheman

Board Regular
Joined
Dec 24, 2020
Messages
55
Office Version
  1. 2007
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".
 

eduzs

Well-known Member
Joined
Jul 6, 2014
Messages
693
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
VBA Code:
Sub Test()
Application.screenupdating=false
With Activesheet.columns("B")
   .ColumnWidth=10
   .WrapText=true
End with
Application.screenupdating=true
End Sub
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,127,589
Messages
5,625,671
Members
416,125
Latest member
NeedExcelHelp2021

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