Excel VBA Resize Table: Step-by-Step Guide
In this article
You resize a table in Excel VBA by setting its Resize property to a new range that includes the header row and all data rows. The quickest working line is ActiveSheet.ListObjects("Table1").Resize ActiveSheet.Range("A1:D20"). That single statement changes the table's boundaries in place, keeping formatting, formulas and the table name intact. The steps below show how to do it reliably, including how to calculate the new range dynamically so the macro does not break when rows are added or removed.
What You'll Need
- Excel on Windows or Mac with macros enabled (the Developer tab visible, or access to the Visual Basic Editor via Alt+F11 on Windows, or Tools > Macro > Visual Basic Editor on Mac).
- A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
- An existing Excel table (Insert > Table) with a known name, such as Table1. You can check the name on the Table Design tab.
- Basic familiarity with the Visual Basic Editor and running a macro.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 on Windows. On Mac, go to Tools > Macro > Visual Basic Editor. Menu labels can differ slightly by Excel version and platform.
- Insert a module. In the editor, choose Insert > Module. This is where the macro will live.
- Identify the table name. Click any cell in the table, then look at the Table Design tab on the ribbon; the name appears in the Table Name box. Alternatively, in the Immediate Window (Ctrl+G) type
?ActiveSheet.ListObjects(1).Nameand press Enter. - Write the simplest resize statement. Type a Sub procedure such as:
Sub ResizeTable()ActiveSheet.ListObjects("Table1").Resize ActiveSheet.Range("A1:D20")End Sub
The range must include the header row. If you pass only data rows, Excel will treat the first data row as the header and you will lose the original headers. - Run the macro. Press F5 while the cursor is inside the Sub, or go back to Excel and use Developer > Macros, select ResizeTable and click Run. The table border should now enclose A1:D20.
- Make the range dynamic. Hard-coded addresses break when data changes. Use the current region or a calculated last row. For example:
Dim ws As WorksheetSet ws = ActiveSheetDim lastRow As LonglastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Rowws.ListObjects("Table1").Resize ws.Range("A1:D" & lastRow)
This assumes column A always has a value in every data row; if not, base the last row on a column that is always populated. - Resize by adding rows only. To extend the table by a fixed number of rows without shrinking it, use:
Dim tbl As ListObjectSet tbl = ActiveSheet.ListObjects("Table1")tbl.Resize tbl.Range.Resize(tbl.Range.Rows.Count + 5)
This adds five rows to the bottom. - Resize to include adjacent columns. If you need to widen the table, build the new range with the same top-left cell but a larger column count, for example
ws.Range("A1:F" & lastRow). The new columns must be empty of other table content or Excel will raise an error. - Save and test. Save the workbook as .xlsm, then run the macro on a copy of your data first. Confirm that headers, formulas and any table style remain correct.
Common Mistakes
- Excluding the header row. The Resize range must start at the header. Passing a data-only range shifts the header down and can corrupt the table structure.
- Overlapping another table or merged cells. Excel will not resize a table over another ListObject or merged cells; you get a run-time error. Clear or move the obstruction first.
- Using the wrong sheet reference.
ActiveSheetdepends on which sheet is visible when the macro runs. PreferThisWorkbook.Worksheets("Sheet1")for reliability. - Assuming the table name. Table names are case-sensitive in some contexts and change if renamed. Read the name rather than guessing.
- Forgetting that resizing down deletes data. If the new range is smaller than the current one, rows outside it are removed from the table (the cells remain, but they are no longer part of the table). Back up first.
Tips
- Use
tbl.Range.Addressin the Immediate Window to see the current boundaries before and after resizing. - Wrap the resize in
On Error GoTohandling so a failed resize does not leave the workbook in an unexpected state. - If the table is the only data on the sheet,
Range("A1").CurrentRegionis a quick way to capture the full block, but it stops at blank rows and columns, so it is not suitable for gapped data. - Turn off screen updating (
Application.ScreenUpdating = False) for large or repeated resizes, then set it back to True. - Keep a copy of the workbook before running any macro that shrinks a table.
Troubleshooting
Run-time error 9 (Subscript out of range): the table name does not exist on the active sheet. Check the spelling and confirm you are on the right worksheet.
Run-time error 1004: usually the new range overlaps another table, merged cells, or a protected sheet. Unprotect the sheet (Review > Unprotect Sheet) and remove overlaps.
The table resizes but formulas break: structured references may point to columns that were removed. Check formulas that use the table name and restore any deleted columns.
Nothing happens: the macro may not have run, or events are disabled. Confirm the Sub name matches the one you selected, and check that Application.EnableEvents is True.
Headers changed: you likely passed a range that did not include the original header row. Undo (Ctrl+Z) and rerun with the header included.
Frequently Asked Questions
Can I resize a table without VBA?
Yes. Drag the small handle at the bottom-right corner of the table, or use Table Design > Resize Table and select the new range.
Does resizing a table delete data outside the new range?
No. The cells remain on the sheet; they simply stop being part of the table. Table formatting and structured references no longer apply to them.
Can I resize multiple tables at once?
Yes, loop through Worksheet.ListObjects and resize each one individually. Each table must not overlap another.
What happens to formulas when I resize?
Formulas inside the retained range stay. Calculated columns may extend into new rows automatically if the column is consistent.
Why does my macro fail on a protected sheet?
Resizing changes the sheet structure, which protection blocks. Unprotect the sheet first, resize, then reprotect if needed.
Can I resize a table to a named range?
Yes, as long as the named range includes the header row and does not overlap another table.