To autofit columns in Excel with VBA, use the Columns.AutoFit method on a specific range, an entire worksheet, or every worksheet in a workbook. The shortest working line is ActiveSheet.Columns.AutoFit, which resizes every column on the active sheet to fit its widest entry. You can run it from the Visual Basic Editor, from the Immediate Window, or from a button on the sheet. No add-ins are required, but macros must be enabled in the workbook.
What You'll Need
- Microsoft Excel on Windows or Mac (the VBA editor is built in; no separate install).
- A workbook saved as
.xlsm(macro-enabled) if you want the code to persist after closing. - Macro security set to allow macros in trusted locations, or the ability to enable content when prompted.
- Basic familiarity with opening the Visual Basic Editor (Alt+F11 on Windows, or the Developer tab on Mac).
Menu labels and the exact location of the Developer tab can differ slightly by Excel version, platform and region. The steps below apply to the desktop versions of Excel; Excel for the web does not run VBA macros.
Step-by-Step
- Open the workbook that contains the columns you want to resize. Save a copy first if the sheet contains data you cannot easily recreate, because a macro that autofits is harmless but a mistyped range reference can still overwrite cells.
- Open the Visual Basic Editor with Alt+F11 (Windows) or by choosing Developer > Visual Basic (Mac and Windows). If the Developer tab is not visible, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the list on the right.
- Insert a standard module: in the editor choose Insert > Module. A blank code window appears. Keeping the code in a standard module makes it easy to run from the Macros dialog or assign to a button.
- Paste the basic autofit macro into the module:
This resizes every column on the active sheet, including hidden ones (they stay hidden but their width changes).Sub AutofitAllColumns() ActiveSheet.Columns.AutoFit End Sub - Run it by pressing F5 while the cursor is inside the subroutine, or choose Run > Run Sub/UserForm. Switch back to Excel (Alt+F11 again) to see the result.
- Autofit only a specific range if you do not want to touch the whole sheet. Replace the body with:
Select the cells first, then run the macro. Only the columns touched by the selection are resized.Sub AutofitSelection() Selection.Columns.AutoFit End Sub - Autofit a named range or table by referencing it directly, for example
Range("A1:D20").Columns.AutoFitorListObjects("SalesTable").Range.Columns.AutoFit. This is safer than relying on the current selection. - Autofit every worksheet in the workbook with a loop:
Note thatSub AutofitAllSheets() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets ws.Columns.AutoFit Next ws End Subws.Columns.AutoFiton a sheet with merged cells may not produce the expected width; see Common Mistakes below. - Autofit rows as well by adding
ActiveSheet.Rows.AutoFitafter the column line. This is useful when wrapped text has made rows too tall or too short. - Save the workbook as .xlsm (File > Save As > Excel Macro-Enabled Workbook). A normal .xlsx file will drop the macro on close, even if it ran fine in the session.
- Optional: assign the macro to a button. On the Developer tab choose Insert > Button (Form Control), draw it on the sheet, and pick your macro from the list. This lets anyone on the sheet resize columns with one click without opening the editor.
Common Mistakes
- Using
Cells.AutoFitinstead ofColumns.AutoFit.Cellsrefers to every cell on the sheet, andAutoFiton a multi-cell range raises an error or does nothing useful. Always call it on aColumnsorRowscollection. - Merged cells. Excel cannot autofit a column that contains merged cells. The column keeps its current width. Unmerge, autofit, then remerge if the layout allows.
- Wrapped text in a single cell. If a cell has Wrap Text on and a long paragraph inside, autofit widens the column to fit the longest line rather than wrapping it. Turn off wrap for that cell, or set a fixed width instead.
- Running on the wrong sheet.
ActiveSheetrefers to whichever sheet is visible when the macro runs. If you run from the editor while a different sheet is active, you resize that one. Use an explicit sheet name such asWorksheets("Data").Columns.AutoFitwhen it matters. - Forgetting to save as .xlsm. The macro runs once, then disappears when the file is reopened as .xlsx.
- Expecting autofit to shrink a column below its current width when the content is short. Autofit does shrink columns, but only to the width of the widest visible entry plus a small margin; a column with a long header will stay wide.
Tips
- Turn off
Application.ScreenUpdatingaround a loop over many sheets to speed it up, then set it back toTrueat the end. - Use
Range("A1").CurrentRegion.Columns.AutoFitto autofit only the contiguous block of data starting at A1, leaving unrelated columns alone. - If you want a maximum width, autofit first and then cap it:
If Columns("A").ColumnWidth > 40 Then Columns("A").ColumnWidth = 40. - Autofit does not change row height when cells contain wrapped text unless you also call
Rows.AutoFit. - Store reusable macros in your Personal Macro Workbook if you want them available in every workbook, rather than copying the module each time.
Troubleshooting
If nothing happens when you run the macro, check the following in order. First, confirm macros are enabled: the yellow security bar at the top of the sheet should say Enable Content, and the Trust Center settings should not be set to Disable all macros without notification. Second, confirm you are looking at the sheet the macro resized; if you used ActiveSheet, the active sheet at the moment of running is the one affected. Third, check for merged cells in the columns you expected to change.
If you get a run-time error, read the highlighted line. Object required usually means Columns was called without a worksheet or range in front of it. Method 'AutoFit' of object 'Range' failed typically points to merged cells or a protected sheet. Unprotect the sheet (Review > Unprotect Sheet) before running, then protect it again.
If the macro is missing after reopening, the file was saved as .xlsx. Use Save As and choose the macro-enabled format. If the macro runs but the result looks wrong, test it on a copy of the workbook and narrow the range down to a few columns to isolate the problem.
Frequently Asked Questions
Does Columns.AutoFit work on hidden columns?
Yes, it adjusts their width, but they remain hidden until you unhide them. To skip hidden columns, loop over visible columns only.
Can I autofit columns in Excel for the web?
No. Excel for the web does not run VBA macros. Use the desktop app, or resize columns manually by double-clicking the column border.
Why does autofit not shrink my column?
The widest entry in that column, including a long header or a cell with Wrap Text off, sets the minimum width. Check for a long value in a cell you did not notice.
Will autofit change the column width of every sheet in the workbook?
Only if you write the loop shown in step 8. ActiveSheet.Columns.AutoFit affects one sheet only.
Can I undo an autofit run?
Yes, Ctrl+Z undoes the last autofit in most cases, but a macro that changes many sheets may not be fully reversible. Work on a copy if the layout matters.
Does autofit work on protected sheets?
No. Unprotect the sheet first, run the macro, then protect it again. Column width changes are blocked while protection is on.