Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

VBA Resize Table to Fit Data: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can resize an Excel table to match its data in VBA by setting the table's Resize method to the exact range you want, or by using CurrentRegion to auto-detect the data block. The shortest working approach is ActiveSheet.ListObjects("Table1").Resize Range("A1:D20") — replace the range with your actual data boundaries. This guide walks through both a fixed-range method and a dynamic method that adapts to changing data, plus what to do when the table won't cooperate.

What You'll Need

  • Excel with a table already created (select your data and press Ctrl+T, or use Insert > Table).
  • The Developer tab enabled: File > Options > Customize Ribbon, then tick Developer in the right-hand list. Labels can differ slightly by Excel version and platform.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows; on Mac, Tools > Macro > Visual Basic Editor).
  • A backup copy of your workbook before running any macro that changes table structure.

Step-by-Step

  1. Identify your table's name. Click any cell inside the table and look at the Table Design tab (called Table on Mac). The name appears in the Table Name box on the left. The default is usually Table1, Table2, and so on. Note it exactly, including any spaces.
  2. Open the VBA editor. Press Alt+F11 (Windows) or go to Tools > Macro > Visual Basic Editor (Mac). In the Project pane, find your workbook and right-click it, then choose Insert > Module. A blank code window opens.
  3. Write the fixed-range resize macro. Paste this, changing the table name and range to match your sheet:
    Sub ResizeTableFixed()
        Dim tbl As ListObject
        Set tbl = ActiveSheet.ListObjects("Table1")
        tbl.Resize tbl.Parent.Range("A1:D20")
    End Sub
    The range must include the header row and all data rows. The top-left cell of the range must stay the same as the table's current top-left cell — you cannot move a table with Resize, only grow or shrink it.
  4. Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, select ResizeTableFixed, and click Run. The table borders and formatting should now cover exactly A1:D20.
  5. For a dynamic resize, use CurrentRegion. If your data grows or shrinks and you want the table to follow automatically, replace the fixed range with the current region of a starting cell:
    Sub ResizeTableDynamic()
        Dim tbl As ListObject
        Set tbl = ActiveSheet.ListObjects("Table1")
        tbl.Resize tbl.Parent.Range("A1").CurrentRegion
    End Sub
    CurrentRegion expands from A1 until it hits blank rows and columns. This works well when your data is a clean block with no gaps. If there are blank rows inside the data, CurrentRegion will stop early and the table will be too small.
  6. Test with sample data. Add a few rows below the table, run the dynamic macro, and confirm the table expands. Delete rows and run it again to confirm it shrinks. If the table does not change, check that the table name in the code matches the name in the Table Name box exactly.
  7. Save the workbook as .xlsm. A workbook containing macros must be saved in the macro-enabled format. File > Save As, then choose Excel Macro-Enabled Workbook (*.xlsm). Saving as .xlsx will strip the code.

Common Mistakes

  • Using a range that doesn't include the header row. The Resize range must start at the header. If you start one row below, the first data row becomes the header and your column names are lost.
  • Trying to move the table. Resize only changes the size, not the position. The top-left cell must remain the same. To move a table, cut and paste the whole table range instead.
  • Wrong table name. Table names are case-sensitive in the sense that they must match exactly, including spaces. Use the Name Box or the Table Design tab to confirm.
  • Assuming CurrentRegion handles gaps. As noted, blank rows or columns break the region. For data with gaps, build the range explicitly using Cells(Rows.Count, 1).End(xlUp).Row to find the last row.
  • Running the macro on the wrong sheet. ActiveSheet refers to whatever sheet is visible. If you switch sheets and run the macro, it may fail or resize a table on another sheet. Qualify the sheet explicitly, e.g. Worksheets("Data").ListObjects("Table1").

Tips

  • Use a variable for the last row and column so the macro adapts: lastRow = Cells(Rows.Count, 1).End(xlUp).Row then Range("A1:D" & lastRow).
  • Wrap the resize in On Error GoTo handling so a missing table or invalid range gives a clear message instead of a debug prompt.
  • If you only need to add rows, ListRows.Add is often safer than Resize because it preserves formulas and formatting in the new row.
  • Record a macro while manually resizing a table to see the exact syntax Excel generates for your version — it's a quick way to confirm the method name and arguments.

Troubleshooting

Run-time error 9: Subscript out of range. The table name doesn't exist on the active sheet. Check the name and the sheet, and remember names are per-sheet.

Run-time error 1004: Resize method of ListObject failed. The range you supplied doesn't overlap the existing table's top-left cell, or it includes cells outside the sheet. Confirm the range starts at the same top-left cell as the table.

The table resizes but formatting looks wrong. Resize keeps the table style but may not copy formulas into new rows. Check a newly added row for formulas; if missing, fill them down manually or use ListRows.Add.

Nothing happens when you run the macro. Macros may be disabled. Check File > Options > Trust Center > Trust Center Settings > Macro Settings and enable macros for this session. Also confirm you saved as .xlsm.

CurrentRegion selects too much or too little. Inspect for blank rows or columns adjacent to the data. Remove them or switch to an explicit last-row calculation.

Frequently Asked Questions

Can I resize a table without VBA?

Yes. Click the table and drag the small handle at the bottom-right corner, or use Table Design > Resize Table and type the new range. VBA is only needed for automation or repeated resizing.

Does Resize keep my formulas and formatting?

It keeps the table style and existing cell content. Formulas in newly included rows are not automatically filled; you may need to copy them down or add rows via ListRows.Add.

Can I resize multiple tables at once?

Yes, loop through ActiveSheet.ListObjects and call Resize on each, but you must supply a valid range for each table individually. There is no built-in bulk resize.

Why does my macro work on one sheet but not another?

Because ActiveSheet changes with the visible sheet. Qualify the worksheet in the code, or activate the correct sheet before running.

What happens if I resize to a range smaller than the data?

Rows outside the new range are removed from the table but the cell data remains on the sheet. The table simply no longer covers them, so they lose table formatting and structured references.

Is Resize available in Excel for Mac?

Yes, the ListObject.Resize method works in Excel for Mac, though the VBA editor is accessed through Tools > Macro > Visual Basic Editor rather than Alt+F11.