View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
[email protected] nomail1983@hotmail.com is offline
external usenet poster
 
Posts: 58
Default How to Autofit column when cells have Wrapped Text?

Using macro record mode, I created a macro that inserts a row, sets
the format alignment to Right with Wrap Text selected, then inserted
titles in each cell, some which include chr(10) to create multi-line
titles. The VBA looks like this:

Rows("1:1").Select : Selection.Insert Shift:=xlDown
With Selection
.HorizontalAlignment = xlRight : .VerticalAlignment = xlBottom
.WrapText = True : .Orientation = 0 : .AddIndent = False
.IndentLevel = 0 : .ShrinkToFit = False : .ReadingOrder =
xlContext
.MergeCells = False
End With
Range("A1").Select : ActiveCell.FormulaR1C1 = "Year"
Range("B1").Select : ActiveCell.FormulaR1C1 = "Your" & Chr(10) &
"Age"
[... etc ...]

Since some of the original columns are not wide enough, the text wraps
within the too-narrow limits of the cell.

I tried to correct that by manually highlighting all columns, then
clicking on Format Column Autofit Selection. That generated the
following VBA code:

Columns("A:N").Select : Selection.Columns.AutoFit

But that failed to widen the columns to accomodate the text up to the
line break [chr(10)].

I can widen the columns to accomodate the text up to the line break,
both manually and in VBA?

(Actually, if you tell me how to do it manually, I can learn the VBA
using macro record mode.)