To autofit columns in Excel with code, set the column's AutoFit property to True in VBA: Columns("A:C").AutoFit. In Office Scripts use worksheet.getRange("A:C").getFormat().autofitColumns(). Both make each column as wide as its widest entry, so text stops getting cut off and you never drag a column border by hand again.

What You'll Need

  • Excel on Windows or Mac (VBA works on both; Office Scripts needs Excel for the web or a Microsoft 365 subscription).
  • The workbook you want to change, ideally saved first.
  • The Developer tab enabled if you are using VBA: File > Options > Customize Ribbon, tick Developer.
  • Basic comfort with the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on Mac) or the Automate tab for Office Scripts.

Step-by-Step

  1. Open the workbook and press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to open the VBA editor. Menu labels can differ slightly by version and region.
  2. Insert a module: in the editor choose Insert > Module. A blank code pane appears.
  3. Paste a basic autofit macro. This fits every column on the active sheet:
    Sub AutoFitAllColumns()
        Cells.EntireColumn.AutoFit
    End Sub
  4. Run it: click inside the macro and press F5, or go back to Excel and use Developer > Macros, select AutoFitAllColumns, and click Run. Column widths adjust instantly.
  5. Limit it to a range if you only want part of the sheet fixed. Replace the body with Range("A1:D20").EntireColumn.AutoFit. This is safer on sheets with wide notes far to the right.
  6. Autofit rows too when wrapped text is clipped vertically: add Cells.EntireRow.AutoFit on the next line.
  7. Save as a macro-enabled file: File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm). Plain .xlsx files drop the macro on save.
  8. For Office Scripts instead, open the Automate tab, choose New Script, and paste:
    function main(workbook: ExcelScript.Workbook) {
        let sheet = workbook.getActiveWorksheet();
        sheet.getUsedRange().getFormat().autofitColumns();
        sheet.getUsedRange().getFormat().autofitRows();
    }
    Click Run. This is the cross-platform route when VBA is unavailable, such as Excel for the web.
  9. Test on a copy before running either script on a workbook you care about, because autofit changes layout and can undo deliberate column widths.

Common Mistakes

  • Saving as .xlsx. The macro vanishes on close. Always use .xlsm for VBA.
  • Running autofit on the whole sheet when a stray value sits in column XFD. Excel widens that column to fit the stray text, which looks like nothing happened. Select the real data range instead.
  • Forgetting merged cells. Autofit ignores merged cells, so a merged header stays clipped. Unmerge, autofit, then remerge if needed.
  • Expecting wrapped text to shrink columns. Autofit sizes to the longest line, not the wrapped block; use row autofit for wrapped cells.
  • Macro security blocking the run. If macros are disabled, Excel shows a security bar; enable macros for that file only, and never for files from unknown sources.

Tips

  • Add Columns("A:A").ColumnWidth = 10 after autofit to cap a column that autofit made too wide.
  • Loop over sheets to fix a whole workbook: For Each ws In Worksheets: ws.Cells.EntireColumn.AutoFit: Next ws.
  • Assign the macro to a button (Developer > Insert > Button) so you can rerun it after edits with one click.
  • Keep the autofit line at the end of any formatting macro so widths reflect the final content.
  • Office Scripts can be scheduled or run from Power Automate, which VBA cannot do natively.

Troubleshooting

Nothing changes. Check you ran the macro on the intended sheet, not another workbook. Also confirm the sheet is not protected; unprotect it via Review > Unprotect Sheet if you have the password.

Columns become enormous. A long text string or a formula result is forcing the width. Find the widest cell in that column and shorten it, or set a fixed width instead of autofitting.

Macro will not run. Close and reopen the file, confirm the .xlsm format, and check Trust Center macro settings. If the file came from elsewhere, use the security bar to enable content for that session only.

Office Script fails. Make sure you selected a worksheet before running; the script above uses the active sheet. If the Automate tab is missing, your Excel version or licence does not include Office Scripts, so use VBA or autofit manually by double-clicking a column border.

Frequently Asked Questions

Can I autofit columns without a macro?

Yes. Select the columns, then double-click any border between two column headers, or use Home > Format > AutoFit Column Width. The code route is for repeating the job automatically.

Does autofit work on merged cells?

No. Excel skips merged cells, so unmerge them, autofit, then merge again if the layout requires it.

Will autofit change my print layout?

It can. Wider columns may push content onto extra printed pages, so check Page Layout > Print Area and scaling after running the macro.

What is the difference between VBA and Office Scripts here?

VBA is desktop-only but works in older files; Office Scripts runs in Excel for the web and Microsoft 365 and can be automated through Power Automate. Both use an autofit method, just different syntax.

Can autofit make columns narrower as well as wider?

Yes. Autofit sets the width to the widest entry in the column, so a column previously widened by hand will shrink back if its content is short.

Does autofit affect hidden columns?

Hidden columns stay hidden, but their widths are recalculated. Unhide them afterward if you need to see the result.