Excel VBA treats a formatted table as a ListObject, and that object is your way in: reference it by name through the worksheet, read or write its DataBodyRange, and add rows with ListRows.Add. Working through the object instead of hard-coded cell addresses means your code keeps running when someone inserts a column, sorts the data or adds rows at the bottom. This guide assumes you already have a table on a sheet, or that you know how to create one with Insert > Table, and that you can open the VBA editor with Alt+F11 and type code into a standard module inserted with Insert > Module.

What You'll Need

  • Excel with macros enabled (desktop Excel on Windows or Mac; the VBA editor is not available in Excel for the web).
  • A worksheet with a real Excel table created via Insert > Table, not just formatted cells. A table has a name shown in the Table Design tab, and every table is automatically named Table1, Table2 and so on unless you rename it.
  • Macro security set so macros can run: File > Options > Trust Center > Trust Center Settings > Macro Settings. Menu wording varies slightly between Excel versions, so look for the closest match in your build.
  • Your file saved as an .xlsm workbook so the code is kept.

Step-by-Step

  1. Give the table a stable name. Click any cell in the table, open Table Design, and type a name in the Table Name box, for example tblOrders. Names cannot contain spaces. This matters because code should refer to the table by name, not by the range it happens to occupy today.
  2. Identify the table in code. Tables belong to the worksheet, not the cell grid, so write Dim lo As ListObject: Set lo = ThisWorkbook.Worksheets("Orders").ListObjects("tblOrders"). If the name is wrong VBA stops with a subscript-out-of-range error, which is the first thing to check when a macro suddenly breaks.
  3. Read the useful ranges. A ListObject exposes three ranges: lo.Range is everything including the header row, lo.HeaderRowRange is the header only, and lo.DataBodyRange is the data without headers. Looping over lo.DataBodyRange.Rows is the normal pattern, and it automatically ignores rows you add later.
  4. Count things safely. Use lo.ListRows.Count for the number of data rows and lo.ListColumns.Count for the column count, rather than counting rows on the sheet. Note that DataBodyRange is Nothing when a table contains only headers, so guard the count with an If Not lo.DataBodyRange Is Nothing check before looping.
  5. Add a record with ListRows.Add. lo.ListRows.Add appends a row at the bottom and copies down formats and formulas. To fill it, use Set r = lo.ListRows.Add then r.Range(1, 1).Value = "A-1001". You can also write the whole row at once from an array, which matters for speed on large writes.
  6. Refer to columns by name, not position. lo.ListColumns("Total").Index gives you the current column number, so code still works if columns are reordered. Combine it with the row number when writing structured references or building formulas.
  7. Filter and sort through the object. Set lo.Range.AutoFilter Field:=lo.ListColumns("Status").Index, Criteria1:="Open" to filter, and clear it with lo.AutoFilter.ShowAllData wrapped in an error check because it fails when nothing is filtered. Sorts use lo.Sort.SortFields; add a key, set its order, then call lo.Sort.Apply.
  8. Delete rows from the bottom up. When deleting matches in a loop, step backwards: For i = lo.ListRows.Count To 1 Step -1 and remove with lo.ListRows(i).Delete. Deleting forwards skips rows because the numbering shifts every time a row disappears.
  9. Work with structured references. Formulas inside a table use names like =[@Qty]*[@Price]. From VBA you can assign that formula string directly to a column's DataBodyRange.Formula and it fills every row at once, which is far quicker than writing cell by cell.
  10. Resize and refresh. lo.Resize lo.Range.Resize(lo.Range.Rows.Count + 5) grows the table by five rows, and lo.Refresh applies the current filter and sort settings. If the table is fed by a query, refreshing the ListObject does not re-run the query unless you also refresh the connection.
  11. Turn headers off when you need raw data. lo.ShowHeaders = False hides the header row if a downstream process needs plain data. Set it back to True afterwards, since the header is what label-based references depend on.
  12. Test against a copy of the workbook. Run the macro, add a column and a row, then run it again. If it still works, the code is properly tied to the table rather than to fixed cells.

Common Mistakes

The most frequent problem is referencing cells directly, such as Range("A2:D50"). That range goes stale the moment someone adds a row, and it silently includes blank rows or misses new ones. Use the ListObject ranges instead.

Second is assuming a table starts at row 1 or column A. A table can begin anywhere, so offsets must come from lo.Range.Row and lo.Range.Column, not from fixed numbers. Third is forgetting that inserting a column inside a table shifts the table's own column numbering but not its names, which is exactly why ListColumns("Name") beats Range(1, 4).

Fourth is a missing table name: if a user renames the table in the Excel interface, hard-coded string names fail. Either keep the name protected or look the table up by index. Fifth, deleting rows forwards in a loop skips records, as noted above. Sixth, writing cell by cell into a large table is slow; assigning an array or a formula to a whole column range is dramatically faster. Finally, remember that a table with only headers has no DataBodyRange, which is a common source of error 91 in otherwise correct code.

Tips

  • Add Option Explicit at the top of the module; it forces you to declare variables and catches typos in table and column names early.
  • Wrap lookups in a small helper that returns Nothing if the table is missing, and handle that case with a clear message rather than letting the macro crash.
  • Use Application.ScreenUpdating = False before large loops and set it back to True at the end; Excel runs noticeably faster when it is not redrawing every change.
  • Convert the table to a range deliberately with lo.Unlist only when you truly need plain cells, because structured references and automatic growth stop working afterwards.
  • Iterate rows with For Each r In lo.DataBodyRange.Rows when you do not need the row number, but switch to an index loop when you plan to delete rows.
  • Comment the table names at the top of each module so a future editor knows what the code depends on.

If That Did Not Work

If the macro reports a subscript-out-of-range error on the ListObjects line, the table name or worksheet name is wrong; check both spellings exactly, including case in code that uses names as strings. If the loop over DataBodyRange throws error 91, the table has headers but no data rows, so add the Nothing guard. If a macro runs but changes nothing, verify macros are enabled and that you are editing a table rather than an ordinary formatted range. If results are wrong after a sort, remember that row indices change, so collect the data you need before sorting rather than after.

Troubleshooting

When code fails, reopen the workbook, press Alt+F11 and use Debug > Compile to catch syntax problems before running. Step through with F8 to see exactly which line stops and what the immediate window shows. Check that the table still exists and still has the expected name by typing ?ThisWorkbook.Worksheets("Orders").ListObjects.Count in the immediate window.

If writes land in the wrong column, print lo.ListColumns("Total").Index to confirm the column order. If the table does not grow as expected, check that you used ListRows.Add rather than writing to the cell just below the table, which leaves the table unchanged. If the file prompts about macros every time, it may need to be in a trusted location; that setting lives in Trust Center and the exact path differs by version. A damaged workbook can sometimes be repaired by saving a copy under a new name before retrying.

Frequently Asked Questions

What is the difference between a table and a range in VBA?

A table is a ListObject with a name, headers, structured references and automatic growth. A plain range is just cells, so it does not resize itself when data is added.

Why does my code say the table does not exist?

Almost always a name mismatch: the worksheet name, the table name, or both. Table names cannot contain spaces, and Excel may have named it Table1, Table2 and so on instead of your chosen name.

Can I loop through table rows without knowing the row count?

Yes. Use For Each r In lo.DataBodyRange.Rows, but guard against an empty table first.

Does adding a row with VBA copy down formulas?

Yes, ListRows.Add normally carries formats and formulas down from the row above, though you can pass a flag to control this.

How do I remove a table but keep the data?

Call lo.Unlist. The cells stay, but the table object disappears, so structured references and automatic growth are gone.

Why is my macro slow on a big table?

Cell-by-cell writes are the usual cause. Assign an array to the whole data range in one go, and switch off screen updating during the operation.