You can automatically resize all columns in an Excel worksheet to fit their widest cell content using a simple VBA script. This process requires a standard .xlsx or .xlsm workbook, as the VBA editor is built into the application. No external software or plugins are necessary to write or run this code.
What You'll Need
- A computer running Microsoft Excel (2010, 2013, 2016, 2019, 2021, or Microsoft 365).
- An existing spreadsheet containing data where the columns are currently too narrow.
- The ability to enable the Developer tab in the Excel ribbon, which houses the VBA editor.
Step-by-Step
- Open the Developer Tab. If you do not see the Developer tab at the top of your Excel window, go to the File > Options > Customize Ribbon. In the right-hand pane, check the box next to Developer and click OK.
- Open the VBA Editor. Once the Developer tab is visible, click on it and select Visual Basic. This will open the VBA window, which allows you to write and run scripts.
- Insert a New Module. In the VBA window, go to the top menu and click Insert > Module. A blank code window will appear on the right side.
- Copy and Paste the Code. Copy the code block below and paste it into the blank module window. Ensure you paste it within the white area of the editor, not inside any existing subroutines or functions.
- Close the VBA Editor. Once the code is pasted, you can minimize the VBA window or close it entirely. You do not need to save the VBA project as a separate file; the code is now attached to your workbook.
- Run the Macro. Return to your Excel worksheet. Go to the Developer tab again and click on Macros. In the dialog box that appears, select AutoFitAllColumns from the list and click Run.
- Verify the Results. The code will execute immediately. All columns across every worksheet in your workbook will resize to fit the width of the widest text or number in their respective columns. You can now read the data without horizontal scrolling.
Sub AutoFitAllColumns()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Columns.AutoFit
Next ws
End Sub
Common Mistakes
When working with VBA macros in Excel, users often encounter specific issues that prevent the code from running as expected. Understanding these pitfalls can save time and frustration.
Running the Macro on a Single Sheet
Many users try to run the macro while the cursor is on a specific sheet, expecting it to only affect that sheet. By default, the code above iterates through every worksheet in the active workbook. If you have hidden sheets, they will also be processed. If you only want to target the current sheet, you must use ActiveSheet.Columns.AutoFit instead of the loop.
Ignoring Security Settings
If you have just copied and pasted the code but the macro does not appear in the Developer > Macros list, your Excel security settings may be blocking it. By design, Excel prevents macros from running if the workbook is not saved as an .xlsm file (Macro-Enabled Workbook). If you are using a standard .xlsx file, Excel will not persist the VBA code. Save your file as .xlsm to fix this.
Forgetting to Save as Macro-Enabled
Even if the code runs once, you cannot reuse it later unless you save the file correctly. Attempting to save a standard .xlsx file will strip out the VBA code, effectively deleting the macro you just created. Always save with the .xlsm extension.
Tips
Apply to All Sheets in One Go
The loop structure used in the code is efficient for cleaning up an entire workbook at once. If you have reports generated from different templates, running this script once ensures every page looks consistent without manual adjustment.
Handling Hidden Rows and Sheets
The standard AutoFit function looks at all visible data. However, if you have a sheet with many hidden rows and you expand them, the AutoFit command generally recalculates the width correctly. If you have sheets that are hidden or very large, it is good practice to unhide them before running the script to ensure no data is left unformatted.
Combine with Row AutoFit
While this guide focuses on columns, you can easily modify the code to autofit rows as well. To do this, change ws.Columns.AutoFit to ws.Rows.AutoFit. Be careful with this, as autofitting rows can make a spreadsheet very tall and difficult to navigate.
Troubleshooting
If the columns do not autofit after following the steps, check the following common causes.
The Macro Does Not Appear in the List
If you go to Developer > Macros and do not see your AutoFitAllColumns macro, it is likely that the code was not pasted into a standard Module. It may have been pasted into a UserForm or a Worksheet object. Ensure you are looking at the VBA editor window, and you have inserted a Module (Insert > Module) before pasting the code.
Error: 'Compile Error: Expected End of Statement'
This error usually happens if the code was not pasted cleanly. Sometimes, copy-pasting from a web browser can introduce invisible characters or line breaks that break the script. If you see this error, close the VBA editor, reopen it, and paste the code again, or type it out manually to ensure it matches the example exactly.
Columns Are Still Too Narrow
If the columns are still too narrow, it may be because the data contains special characters or is formatted as text rather than numbers. Additionally, if the cells contain formulas that return an error (like #DIV/0!), the autofit logic might be struggling to determine the width. Double-check that the data in the cells is visible and does not contain syntax errors.
Code Runs but Doesn't Work on Specific Sheets
Check if the sheets you are targeting are protected. If a worksheet is protected with a password, VBA cannot modify the column widths. You will need to unprotect the sheet (Review > Unprotect Sheet) before running the macro, or remove the protection entirely.
Frequently Asked Questions
Can I use this code in Excel for Mac?
Yes, the VBA editor and the Columns.AutoFit method function identically on the Mac version of Excel. The steps to open the Developer tab and insert a module are the same.
Does this code delete my data?
No. This code only adjusts the visual width of the columns. It does not change the underlying data, formatting, or cell contents in any way.
How do I run the macro without the Developer tab?
You can press Alt + F11 on your keyboard to open the VBA editor directly. Once the editor is open, press Ctrl + G to open the Immediate window, type AutoFitAllColumns, and press Enter to run it.
Can I attach this to a button on the sheet?
Yes. After running the code once, go to the Developer tab and click Insert > Button (Form Control). Draw the button on your sheet, assign the macro named AutoFitAllColumns, and click OK. Now you can simply click the button to resize all columns instantly.
Why do some columns stay the same size?
If a column has a custom width set and that width is wider than the AutoFit result, Excel will respect the custom setting and ignore the autofit command for that specific column.