Columns("A:D").AutoFit sets columns A to D to the width of their longest entry, and Rows("1:20").AutoFit does the same for row heights. For a whole sheet, Worksheets("Data").UsedRange.Columns.AutoFit sizes only the columns in use, which is quicker than Cells.EntireColumn.AutoFit.
To size columns by part of the data, AutoFit a range’s columns rather than entire columns. Range("A2:D100").Columns.AutoFit sizes A to D by rows 2 to 100 only, so a long title in row 1 doesn’t make column A too wide. Range("A2:D100").EntireColumn.AutoFit would include the title.
Name the sheet when another one might be active, because a bare Columns("A:D") means the active sheet. Run the line after the code that fills the cells; widths don’t adjust themselves when values change later.
Merged cells are ignored, so a column whose longest text sits in a merged cell won’t widen. Row AutoFit adds height for wrapped text, so turn on Wrap Text first; long text without wrapping stays on one line.
Key Points
- In VBA, you can use the AutoFit method to auto-fit rows, columns, and even an entire worksheet.
- You need to specify the range, and then you can use the AutoFit method. The range must be rows or columns (like EntireColumn, Columns or Rows); AutoFit on a plain cell range gives run-time error 1004.
AutoFit a Column
Let’s say you want to autofit column A; the code would be something like below:
Range("A1").EntireColumn.AutoFit

In the above line of code, you have used the EntireColumn property to refer to the entire column of cell A1.
As you are within a worksheet, you can also use the Columns property and write code like the below.
Columns(1).AutoFit

AutoFit a Row
In the same way, you can write code to autofit a row. Let’s say you want to autofit row 5; the code would be:
Range("A5").EntireRow.AutoFit

And if you want to use the Rows property, then you can use code like the following.
Rows(5).AutoFit

AutoFit UsedRange (Rows and Columns)
Now let’s say you only want to autofit those columns and rows where you have data. In VBA, there is a property called UsedRange that you can use. So the code would be:
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.UsedRange.EntireRow.AutoFit

And if you want to use a specific worksheet, then the code would be:
Worksheets("Sheet1").UsedRange.EntireColumn.AutoFit Worksheets("Sheet1").UsedRange.EntireRow.AutoFit

AutoFit Entire Worksheet
And if you want to refer to all the columns and rows of the worksheet, then you can use the “CELLS” property. Here’s the code.
Worksheets("Sheet1").Cells.EntireColumn.AutoFit Worksheets("Sheet1").Cells.EntireRow.AutoFit

Or, you can also use VBA’s WITH statement to write a code like the one below.
With Worksheets("Sheet1").Cells .EntireColumn.AutoFit .EntireRow.AutoFit End With

Like the other short codes on this page, put these lines between Sub and End Sub in a module to run them.
AutoFit Multiple Columns or Only Part of the Data
You can autofit several columns in one line. And when you use the Columns of a smaller range, Excel fits the width only to the cells in that range. That’s useful when a long title in row 1 shouldn’t make the whole column wide:
Sub autofit_columns()
Columns("A:D").AutoFit 'fits to all the cells in A:D
Range("A2:D100").Columns.AutoFit 'fits only to rows 2 to 100
End Sub
AutoFit with a Maximum Width
A very long value can make a column too wide. The following code autofits the columns of the used range and then limits each one to a width of 50:
Sub autofit_max_width()
Dim col As Range
For Each col In ActiveSheet.UsedRange.Columns
col.AutoFit
If col.ColumnWidth > 50 Then col.ColumnWidth = 50
Next col
End Sub
When AutoFit Doesn’t Work
- Merged cells: AutoFit ignores merged cells, so set the width or height yourself with ColumnWidth or RowHeight.
- Wrapped text: to fit the row height to wrapped text, autofit the rows (not the columns) after you turn on wrap text.
- Not rows or columns: Range(“A1:D10”).AutoFit gives error 1004. Add .Columns or .Rows before AutoFit.
Frequently Asked Questions
Can Excel autofit columns automatically when data changes?
Yes, with a Worksheet_Change event. Put Me.UsedRange.Columns.AutoFit in the Worksheet_Change procedure of that sheet’s code module, and it runs every time you change a cell.
What is the difference between AutoFit and ColumnWidth?
AutoFit sets the width based on the content of the cells. ColumnWidth sets a fixed width that you choose, like Columns(“A”).ColumnWidth = 20, whatever the content is.
How do I autofit on a sheet that is not active?
Refer to the sheet first, like Worksheets(“Sheet2”).Columns(“A:D”).AutoFit. You don’t need to activate the sheet.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.