Smart Systems for Home

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

Excel VBA Table Array: A How-To Guide

· 5 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

To use a table array in Excel VBA, you define a Range object that points to your data table and then refer to that range in your code. The simplest working approach is to set a variable to the table's range using Range("A1:C10") or ListObjects("TableName").Range, then pass that variable to functions like VLookup or loop through its rows. No add-ins are required; everything runs in the built-in Visual Basic Editor.

What You'll Need

  • Excel for Windows or Mac with macros enabled (the steps below use the standard VBA editor; labels can differ slightly by version or region).
  • A worksheet containing a structured data table with clear headers in the first row.
  • Basic familiarity with opening the Visual Basic Editor (Alt+F11 on Windows; on Mac use Tools > Macro > Visual Basic Editor).

Step-by-Step

  1. Prepare your table. Put your data in a contiguous block with headers in row 1. For example, A1:C10 where column A holds product names, B holds prices, and C holds stock counts. Avoid blank rows or columns inside the block; they break range references.
  2. Open the Visual Basic Editor. Press Alt+F11 (Windows) or use the Developer tab > Visual Basic. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it.
  3. Insert a module. In the editor, choose Insert > Module. This creates a blank code window where your macro will live.
  4. Declare a Range variable. Type Dim tbl As Range at the top of your procedure. This variable will hold the table array reference.
  5. Set the range to your table. Use Set tbl = Worksheets("Sheet1").Range("A1:C10"). Replace the sheet name and address with your actual data. If your data is an Excel Table (created with Insert > Table), use Set tbl = Worksheets("Sheet1").ListObjects("Table1").Range instead. The ListObjects approach automatically expands when rows are added.
  6. Use the array in a lookup. A common use is VLookup: result = Application.WorksheetFunction.VLookup("Widget", tbl, 2, False). The tbl variable is the table array argument. The 2 means return the second column, and False means exact match.
  7. Loop through the table if needed. To process every row, use For Each cell In tbl.Columns(1).Cells and act on each value. This is useful for summing, filtering, or writing results to another sheet.
  8. Run the macro. Press F5 or choose Run > Run Sub/UserForm. Check the Immediate Window (Ctrl+G) for output if you use Debug.Print.
  9. Save your work. Save the workbook as .xlsm (macro-enabled) so the code persists. A standard .xlsx save will strip the macro.

Common Mistakes

  • Forgetting Set. Writing tbl = Range("A1:C10") without Set causes a runtime error because Range is an object. Always use Set tbl = ....
  • Hard-coding the range. If you add rows later, a fixed address like A1:C10 misses them. Use a ListObject range or a dynamic last-row calculation.
  • Including headers in the lookup array. VLookup with exact match will not find a header value, but including headers shifts your column index. Start the range at the first data row if you want column 1 to be the lookup column.
  • Wrong sheet reference. If you omit the worksheet qualifier, VBA uses the active sheet, which may not be the one you expect. Always name the sheet.
  • Mismatched data types. Looking up a number stored as text (or vice versa) returns an error. Check with IsNumeric or convert values before lookup.

Tips

  • Use ListObjects("TableName").Range for tables created with Insert > Table; it grows automatically and avoids manual address edits.
  • Name your ranges (Formulas > Define Name) and refer to them in VBA with Range("MyRange") for readability.
  • Use Application.WorksheetFunction.VLookup only when you are sure the value exists; otherwise wrap it in error handling with On Error Resume Next and check the result.
  • For large tables, read the range into a Variant array first (arr = tbl.Value) and loop through the array in memory; this is much faster than reading cell by cell.
  • Use Debug.Print tbl.Address to confirm the range is what you think it is before running the rest of the code.

Troubleshooting

  • "Subscript out of range" error. The sheet name or table name does not match. Check spelling and confirm the sheet exists in the active workbook.
  • VLookup returns #N/A. The lookup value is not in the first column of the table array, or there is a type mismatch. Verify with a manual lookup on the sheet.
  • Macro does nothing. Check that macros are enabled (File > Options > Trust Center > Macro Settings) and that you are running the correct procedure.
  • Range address is wrong. Print tbl.Address to the Immediate Window. If it shows a different sheet, your worksheet reference is off.
  • Code works on one machine but not another. Check Excel version and whether the workbook is saved as .xlsm. Also confirm the table name is identical; renaming a table breaks ListObjects references.

Frequently Asked Questions

What is a table array in VBA?

A table array is simply a Range object that contains your data table. In VBA you assign it to a variable and pass it to functions like VLookup or loop through it.

Can I use a named range as a table array?

Yes. Define a name on the sheet (Formulas > Define Name), then use Range("MyRange") in your code. This keeps the reference readable and easy to update.

How do I refer to an Excel Table by name in VBA?

Use Worksheets("Sheet1").ListObjects("Table1").Range. The table name is the one shown on the Table Design tab, not the sheet name.

Why does my VLookup fail inside VBA but work on the sheet?

Usually a type mismatch or a wrong column index. Ensure the lookup value matches the data type in the first column and that the column number is correct.

Do I need to enable macros to use table arrays?

Yes. Any VBA code requires macros to be enabled. Save the file as .xlsm and adjust Trust Center settings if needed.

Can I use a table array with other functions like Match or Index?

Yes. The same Range variable works with Application.WorksheetFunction.Match and Index, giving you flexible lookup combinations.