Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoFit doesn't work with wrapped text

Tags:

excel

vba

I have one Chr(10) in the cell

cell.WrapText = False
cell.EntireRow.AutoFit ' AutoFit works
' ------------ but:
cell.WrapText = True
cell.EntireRow.AutoFit ' AutoFit works only if the cell has less then five lines.

If I add some characters (one line more) - AutoFit doesn't work. Text is cutted on the first and last line.
ver - famous excel 2010

like image 837
Alegro Avatar asked Dec 30 '25 09:12

Alegro


1 Answers

It depends on how big the data is and what is the width of the column. Reason being the max height a row can expand is to 409.5 (546 pixels). If you manually increase the height of the row, you will notice that after a particular height, you will not be able to increase the height of the row. Check what is that height :) Same is the concept with the width. This is applicable to both rows and columns. In case of columns the max width is 254.86 (1789 pixels)

I used this code for demonstration purpose.

Option Explicit

Sub Sample()
    ActiveCell.WrapText = True
    ActiveCell.EntireRow.AutoFit
    MsgBox ActiveCell.RowHeight
End Sub

See this screenshot

This works

enter image description here

This doesn't

The row has reach it's maximum. It cannot go beyond that.

enter image description here

The best way to handle this is to increase the width of the column.

FOLLOWUP

The reason why it was not working is because you had multiple columns with data in that row and you were trying to just set the WrapText of 1 cell and hence it was not working. When you have multiple columns of data you have to use cell.EntireRow.WrapText = True instead of ActiveCell.WrapText = True

like image 66
Siddharth Rout Avatar answered Jan 02 '26 00:01

Siddharth Rout