Excel VBA Table Loop: Step-by-Step Guide
In this article
To loop through an Excel table with VBA, use a For Each loop over the table's DataBodyRange.Rows or ListColumns, or a For loop with ListRows.Count; this processes only the data rows and ignores headers and totals. This guide shows the exact code patterns, how to reference a table by name, and how to avoid the common pitfalls that make loops skip rows or hit the wrong range. You need Excel with macros enabled and a table already created (select your data and press Ctrl+T).
What You'll Need
- Excel for Windows or Mac with macro support (the Developer tab visible; if not, enable it via File > Options > Customize Ribbon).
- A worksheet containing a formal Excel table (Insert > Table, or Ctrl+T), not just a formatted range.
- The table's name. Click any cell in the table and check the Table Design tab; the name is in the Table Name box (defaults like Table1).
- Basic familiarity with the VBA editor (Alt+F11) and how to insert a module (Insert > Module).
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11, then choose Insert > Module. All code below goes into that module. Menu labels can differ slightly by Excel version and platform; on Mac, use the ribbon's Developer > Visual Basic instead of Alt+F11 if the shortcut does not respond.
- Declare variables for the table and a row. At the top of the procedure write
Dim tbl As ListObjectandDim rw As ListRow. UsingListObjectmeans you refer to the table by name, so the code keeps working if the table moves or grows. - Set the table reference. Use
Set tbl = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1"). Replace Sheet1 with your sheet's name and Table1 with your table's name. If the name is wrong, VBA raises run-time error 9 (Subscript out of range), so copy the name exactly from the Table Name box. - Loop over data rows with For Each. Write
For Each rw In tbl.DataBodyRange.Rows...Next rw.DataBodyRangecovers only the rows between the header and the totals row, so you never process the header. Inside the loop,rw.Range.Cells(1, 1).Valueis the first cell of the current row, andrw.Range.Cells(1, 2).Valueis the second, and so on. - Or loop by column name instead of position. If columns may be reordered, read values with
tbl.ListColumns("Amount").DataBodyRange.Cells(rw.Index, 1).Value. This is more verbose but survives inserted or moved columns, which positionalCells(1, n)references do not. - Use a For loop when you need the row number. Write
Dim i As LongandFor i = 1 To tbl.ListRows.Count. Thentbl.ListRows(i).Range.Cells(1, 1).Valuegives the first column of row i. This pattern is handy when you write results back to a specific row or need to skip rows conditionally. - Add your processing logic inside the loop. A typical task is summing a column or flagging rows. For example, to write "High" into a Status column when the Amount column exceeds 1000:
If tbl.ListColumns("Amount").DataBodyRange.Cells(i, 1).Value > 1000 Then tbl.ListColumns("Status").DataBodyRange.Cells(i, 1).Value = "High". Keep reads and writes on the same row index so results stay aligned. - Guard against an empty table. If the table has only a header,
DataBodyRangeisNothingand the loop fails. AddIf Not tbl.DataBodyRange Is Nothing Thenbefore the loop. This is the single most common cause of run-time error 91 (Object variable or With block variable not set). - Run the macro. Press F5 with the cursor inside the procedure, or assign the macro to a button via Developer > Insert > Button (Form Control). Save the workbook as .xlsm, because .xlsx cannot store macros.
- Verify the results. Check that every data row was processed exactly once and that the header and totals row were untouched. If a row was skipped, see Common Mistakes below.
Common Mistakes
- Looping over the whole table instead of the data body.
tbl.Range.Rowsincludes the header and totals rows;tbl.DataBodyRange.Rowsdoes not. Use the latter unless you deliberately want the header. - Deleting rows inside a forward For loop. When you delete
tbl.ListRows(i), the rows below shift up and the counter skips the next row. Loop backwards (For i = tbl.ListRows.Count To 1 Step -1) when deleting, or collect rows to delete and remove them after the loop. - Assuming DataBodyRange always exists. An empty table returns Nothing. Always test for it first.
- Hard-coding column numbers. Inserting a column shifts every position. Use
ListColumns("Name")for anything that matters. - Forgetting the table name is case-sensitive in practice. VBA may accept different casing, but typos still trigger error 9; copy the name from the Table Name box rather than retyping it.
- Leaving
Application.ScreenUpdatingon. For large tables, set it to False at the start and True at the end; otherwise the loop redraws the sheet on every write and crawls.
Tips
- Read cell values into a VBA array first when the table is large; looping cell by cell through thousands of rows is slow.
- Use
tbl.ListRows.Addto append rows so the table expands automatically instead of writing below it. - Name tables meaningfully (SalesData rather than Table1) so the code reads clearly and survives copy-paste between workbooks.
- Store the column reference once (
Set colAmount = tbl.ListColumns("Amount")) rather than looking it up on every iteration. - Add
Option Explicitat the top of the module to catch undeclared variables before they cause silent bugs.
Troubleshooting
- Run-time error 9 (Subscript out of range): the sheet or table name does not match. Check both names exactly, including spaces.
- Run-time error 91: the table is empty, so
DataBodyRangeis Nothing. Add the guard from step 8. - The loop processes the header row: you used
tbl.Rangeinstead oftbl.DataBodyRange. - Every other row is skipped after a delete: you are deleting inside a forward loop. Loop backwards or delete after collecting rows.
- Nothing happens when you run it: the macro may be in the wrong module or the workbook saved as .xlsx. Save as .xlsm and confirm the procedure name matches the one you run.
- Results land in the wrong column: a column was inserted and your positional
Cells(1, n)reference shifted. Switch toListColumns.
Frequently Asked Questions
How do I loop through only the visible rows of a filtered table?
Check rw.Range.EntireRow.Hidden inside the loop and skip hidden rows, or use tbl.DataBodyRange.SpecialCells(xlCellTypeVisible) and iterate its areas. The SpecialCells approach needs a guard because it errors when no rows are visible.
Can I loop through columns instead of rows?
Yes. Use For Each col In tbl.ListColumns to process each column, then read col.DataBodyRange for its values. This is useful for reformatting or validating every column.
Why does my loop stop at the totals row?
It should not if you use DataBodyRange, which excludes totals. If it does, you are looping tbl.Range.Rows, which includes both header and totals.
Does this work in Excel for Mac?
Yes. The object model for ListObject is the same. Shortcuts differ (use the Developer tab rather than Alt+F11 if needed), and some Windows-only features are unavailable, but table loops work identically.
How do I write results back to the same row?
Use the row index from the loop: tbl.ListColumns("Result").DataBodyRange.Cells(i, 1).Value = ... inside a For i loop, or rw.Range.Cells(1, n) inside a For Each rw loop.
Can I loop a table on another sheet or workbook?
Yes, qualify the reference fully, for example Workbooks("Book2.xlsx").Worksheets("Data").ListObjects("Sales"). The other workbook must be open.