So, today is my bad day
I discovered that excel row AutoFit – doesn’t work
a = "Please" b = "Do AutoFit" range01.Value = a & vbNewLine & b range01.EntireRow.AutoFit // First and last line are horizontally cutted
Chr(10) instead of vbNewLine – doesn’t help
It works only if I write without line breaks:
range01.value = a & b range01.EntireRow.AutoFit // works !!! Thanks to Microsoft !
Make sure that the cells that have line breaks in them have “Wrap Text” turned on. You can turn it on with VBA like so:
Sub test1() Dim a As String, b As String a = "Please" b = "Do AutoFit" Range("A1").Value = a & vbNewLine & b Range("A1").WrapText = True Range("A1").EntireRow.AutoFit End Sub
Autofit doesn’t work on merged cells. My guess is that’s what you are trying to do.
Work around this by simulating the autofit on a single cell with the same width as your merged cell and set the height it gives you.
Something like this:
Dim YourString As String, a As String, b As String Dim TempLoc as Range a = "Please" b = "Do AutoFit" YourString = a & vbNewLine & b Set TempLoc = range01.offset(0, 10) ' Given the column 10 columns to the right has the exact same witdh as your merged cell. ' Autofit a single cell to get correct height With TempLoc .Value = YourString .WrapText = True .EntireRow.AutoFit .RowHeight = TempLoc.RowHeight .ClearContents End With
You could do:
Private Sub Worksheet_Activate() Worksheets("Sheet1").Range("A:A").EntireRow.AutoFit End Sub
I solved an issue with this – I had a row with two merged cells both containing multi-line text with Chr(10) linefeeds – with Wrap set ON (but no lines actually wrap – only the Chr(10)s cause new lines). Autofit wouldn’t work due to the merged cells.
The VBA workaround was to use a spare cell on the same row, and fill it with the same number of Chr(10) as found in one (either) of the multi-line cells. Then call AutoFit for that cell now containing the same number of invisible line feeds. Make sure the font and size are the same in the spare cell!
' make C36 spare cell contain the same number of Chr(10) as D36, using a Repeat function : ' (after counting Chr(10) by comparing the length before and after substituting Chr(10) with "") Worksheets(1).Range("C36").Value = _ Application.WorksheetFunction.Rept(Chr(10), _ Len(Worksheets(1).Range("D36").Value) - Len(Replace(Worksheets(1).Range("D36").Value, Chr(10), ""))) Worksheets(1).Range("C36").Rows.AutoFit
An alternative method for other scenarios could be to use an extra, hidden, worksheet – set a single cell to the same column width as your merged cells, copy the contents over, call autofit (which should work on the unmerged cell), then use the resulting rowheight from that one.
One workaround is to shrink the column width down a bit for the column(s) that are causing the autofit problem, select all the rows you want to change, autofit the row height by double-clicking one of the row separators, and finally autofit the column(s) from the first step by double-clicking the column separator(s). Probably works best if only one or just a few columns consistently have the most text in their cells.