VBA Code to Auto Adjust Column Width in Excel
In this article
To auto-fit column widths in Excel with VBA, use the Columns.AutoFit method on the range you want to resize. The shortest working version is Columns("A:C").AutoFit or ActiveSheet.UsedRange.Columns.AutoFit. This one line widens or narrows each column so the longest entry fits, which is exactly what double-clicking the column border does manually. You do not need any add-in or special setting, only the Developer tab enabled so you can open the Visual Basic Editor.
What You'll Need
- Excel for Windows or Mac (the VBA Editor is included in all desktop versions; Excel for the web does not run macros).
- The Developer tab visible: right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list.
- A workbook saved as .xlsm (macro-enabled) if you want the code to stay in the file.
- Basic familiarity with opening the VBA Editor with Alt+F11 (Windows) or Option+F11 (Mac).
Step-by-Step
- Open the VBA Editor. Press Alt+F11 on Windows or Option+F11 on Mac. The editor opens in a separate window. Menu labels can differ slightly between Excel versions and platforms, but the layout is the same.
- Insert a module. In the editor, go to Insert > Module. A blank code window appears on the right. Code placed in a module can be run from the Macros dialog and reused across sheets.
- Paste the basic auto-fit code. In the blank module, type or paste:
Sub AutoFitColumns()ActiveSheet.UsedRange.Columns.AutoFitEnd Sub
This resizes every column that contains data on the active sheet, leaving empty columns outside the used range untouched. - Run it. Press F5 while the cursor is inside the subroutine, or close the editor and use Developer > Macros, select AutoFitColumns, and click Run. The columns on the active sheet resize immediately.
- Limit it to specific columns if needed. Replace the middle line with
Columns("A:C").AutoFitto resize only A through C, orRange("A1:D20").Columns.AutoFitto resize only the columns spanned by that range. Use the sheet qualifier, for exampleWorksheets("Data").Columns("A:C").AutoFit, when you want to target a sheet that is not active. - Auto-fit every sheet in the workbook. Loop through the sheets:
Dim ws As WorksheetFor Each ws In ThisWorkbook.Worksheetsws.UsedRange.Columns.AutoFitNext ws
This is useful for a report workbook with several tabs. - Run it automatically after data changes. If you want the columns to resize whenever a value is entered, put
Columns.AutoFitinside a Worksheet_Change event in the sheet's code module rather than a standard module. Keep in mind this fires on every edit and can slow down large sheets, so apply it only to the columns you are entering data in. - Save the workbook. Save as .xlsm so the macro is retained. If you save as .xlsx, Excel warns that the macro will be removed.
Common Mistakes
- Using
Cells.AutoFiton the whole sheet. This tries to resize all 16,384 columns and can be very slow. UseUsedRangeor a named range instead. - Forgetting the sheet qualifier.
Columns("A:C").AutoFitacts on the active sheet. If the wrong sheet is active, nothing visible happens. Qualify withWorksheets("Name"). - Auto-fitting wrapped text. AutoFit sizes for the longest unbroken string, not for wrapped lines. If a cell has Wrap Text turned on, the column may still look too narrow; either turn wrapping off or set a fixed width.
- Expecting it to work in Excel for the web. Macros do not run there. Use the desktop app.
- Leaving the workbook as .xlsx. The code disappears on save. Always save as .xlsm.
- Running it on a protected sheet. AutoFit fails if the sheet is protected. Unprotect the sheet first, or run the code before applying protection.
Tips
- Record a macro (Developer > Record Macro) while double-clicking a column border to see the exact code Excel generates; it is a quick way to confirm the method name on your version.
- Add
Application.ScreenUpdating = Falseat the start andTrueat the end of a loop over many sheets to speed things up. - Combine AutoFit with a maximum width check if you have very long text: loop the columns, AutoFit, then cap
ColumnWidthat a sensible number such as 50. - Assign the macro to a ribbon button or a keyboard shortcut via Developer > Macros > Options so you can run it in one click.
- Keep one module named something like
modLayoutfor all formatting macros so they are easy to find later.
Troubleshooting
If nothing happens when you run the macro, first check that the correct sheet is active and that the code is in a standard module, not a sheet module. If you see a "Macros are disabled" message, enable macros for the file or check Trust Center settings. If the code errors with "Method 'AutoFit' of object 'Range' failed", the sheet is probably protected or the range refers to a merged cell; unprotect the sheet and unmerge before running. If columns resize but the text still looks cut off, the cells likely have Wrap Text on or the font is larger than expected; turn wrapping off or widen manually. If the macro is slow, restrict it to UsedRange and turn off screen updating. If the code vanishes after saving, you saved as .xlsx; use Save As and choose .xlsm. If running on Mac, confirm you are in the desktop app rather than Excel for the web, and use Option+F11 to open the editor.
Frequently Asked Questions
What is the VBA code to auto-fit all columns?
Use ActiveSheet.UsedRange.Columns.AutoFit to fit every column with data on the active sheet, or loop ws.UsedRange.Columns.AutoFit over all worksheets to do the whole workbook.
Can I auto-fit only one column?
Yes. Use Columns("B").AutoFit or Range("B:B").AutoFit. For a single cell's column, Range("B5").EntireColumn.AutoFit also works.
Does AutoFit work on merged cells?
No. AutoFit ignores merged cells and can raise an error if the range includes them. Unmerge, fit, then remerge if you need the layout.
Why does AutoFit not shrink my columns?
AutoFit only shrinks a column if the widest entry is shorter than the current width. If a cell contains trailing spaces, a long formula result, or wrapped text, the column may stay wide. Check the cell contents and turn off Wrap Text.
Will this work in Excel for the web or Excel Mobile?
No. VBA macros run only in the Excel desktop application. Excel for the web and the mobile apps do not execute VBA.
How do I run the macro automatically when the workbook opens?
Place a call to your auto-fit subroutine inside the Workbook_Open event in the ThisWorkbook module. The workbook must be saved as .xlsm and macros must be enabled.