Skip to main content

Range.AutoFit method

Changes the width of the columns in the range or the height of the rows in the range to achieve the best fit.

Syntax

expression.AutoFit

expression A variable that represents a Range object.

Return value

Variant

Remarks

The Range object must be a row or a range of rows, or a column or a range of columns; otherwise, this method generates an error.

One unit of column width is equal to the width of one character in the Normal style.

Example

The following example changes the width of columns A through I on Sheet1 to achieve the best fit.

Worksheets("Sheet1").Columns("A:I").AutoFit

The following example changes the width of columns A through E on Sheet1 to achieve the best fit, based only on the contents of cells A1:E1.

Worksheets("Sheet1").Range("A1:E1").Columns.AutoFit

The following example changes the width of entire worksheet.

Cells.EntireColumn.AutoFit

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>