EXCEL AUTOMATION & VBA

How to AutoFit the Used Range with VBA

Use VBA to AutoFit the actual used range in Excel and avoid expanding unused rows or columns unnecessarily.

Excel VBA macro AutoFitting only the used rows and columns of a worksheet
← Tutorial HubExcel Automation & VBA

Plan the macro

Place the procedure in a standard module and target the intended sheet explicitly. If the workbook also needs controlled protection, see the VBA sheet-protection tutorial.

Public Sub AutoFitUsedRange()
    Const SHEET_NAME As String = "Report"
    Dim ws As Worksheet
    Dim lastRowCell As Range
    Dim lastColCell As Range
    Dim usedArea As Range

    On Error GoTo CleanFail
    Set ws = ThisWorkbook.Worksheets(SHEET_NAME)

    Set lastRowCell = ws.Cells.Find(What:="*", After:=ws.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    If lastRowCell Is Nothing Then
        Exit Sub
    End If

    Set lastColCell = ws.Cells.Find(What:="*", After:=ws.Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious)
    Set usedArea = ws.Range(ws.Cells(1, 1), ws.Cells(lastRowCell.Row, lastColCell.Column))
    usedArea.Columns.AutoFit
    usedArea.Rows.AutoFit
    Exit Sub

CleanFail:
    MsgBox "AutoFit failed: " & Err.Description, vbExclamation
End Sub
Excel VBA AutoFit setup identifying the worksheet used range instead of entire rows and columns

Implementation steps

  1. Decide whether widths, heights, or both should change.
  2. Find the last used row and last used column separately.
  3. Limit AutoFit to the rectangle defined by those boundaries.
  4. Apply maximum widths afterward when long notes could create unusable columns.
  5. Review merged cells and wrapped text separately.
  6. Test print layout and dashboard alignment.
Excel VBA AutoFit workflow resizing only populated rows and columns in the used range

Common VBA mistakes

MistakeImpact
Using Cells.EntireColumn.AutoFitEvery worksheet column is processed.
Relying only on UsedRangeOld formatting can make the used area larger than expected.
AutoFitting dashboard columnsShape and chart layouts can shift visually.
Ignoring merged cellsAutoFit does not handle them reliably.
Allowing one long text entry to widen a column excessivelySet practical limits.

Find both the last row and the last column

A true used-area AutoFit should not assume the cell found in the last populated row is also in the rightmost used column. One report might have its last row in column A while formulas or headings extend to column H above it. Find the last used row and last used column separately, then AutoFit the rectangle between A1 and that row-and-column intersection.

After AutoFit, apply practical caps. A long note can make a column hundreds of pixels wide even though wrapped text would be easier to read. Fixed dashboards are a separate case: changing column widths can shift shapes and chart alignment, so keep those layouts controlled. The macro is most appropriate for data tables and reports whose dimensions are driven by cell content, not for presentation sheets with carefully positioned objects.

Keep AutoFit away from fixed presentation layouts

AutoFit is ideal for data sheets where column width should follow content. It is often a poor fit for dashboards whose shapes, buttons, and charts are positioned around fixed columns. If a report mixes both styles, target only the data-table columns and preserve the dashboard grid. A targeted macro is safer than applying AutoFit to the whole worksheet every time the workbook opens. For another reusable automation pattern, see the VBA guide to refreshing queries and PivotTables.

Excel VBA AutoFit verification checking used-range dimensions, wrapped text, headers, and unchanged unused cells

Frequently asked questions

Why is a column still too wide?

A long value or formula result is determining the width.

Can I cap the width?

Yes. Loop through the fitted columns and set a maximum ColumnWidth.

Does this work on Mac?

The Find and AutoFit methods are cross-platform.

Should the macro run automatically?

Only when the layout is data-driven; fixed dashboards usually need controlled dimensions.

Continue building reliable Excel workflows

Browse more lessons in the OneXcel Tutorial Hub or explore free Excel resources.