Compare Dynamic Excel Table Rows Across 12 Columns with VBA
Question details
Compare corresponding rows across two Excel tables whose row counts change, checking 12 columns dynamically.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Two tables contain user-dependent numbers of rows and need to be compared without hard-coding their last rows.
- Observed behavior
- A VBA comparison should detect each table's current range and report differences across 12 columns, including rows present in only one table.
Make sure each table has a header row, at least 12 columns, and no completely blank rows or columns inside its data. Separate the tables from other data with blank rows or columns, and note their upper-left cells.
Detect Both Table Ranges and Compare Their Rows with VBA
Use CurrentRegion to find each table and write a 12-column comparison to a new worksheet.
This macro matches rows by position, not by an ID or other key. It compares the first data row of each table, then the second, and so on.
The example assumes both tables are on the active worksheet, with headers beginning at A1 and AA1. Change those cell references if your tables start elsewhere. Results are written to a new sheet so they do not expand either table's CurrentRegion.
In desktop Excel, open the workbook, select the sheet containing both tables, and choose Developer > Visual Basic. You can also open the editor with Alt+F11 on Windows.
In the VBA editor, choose Insert > Module and paste this code: Sub CompareDynamicTables() Dim ws As Worksheet, results As Worksheet Dim table1 As Range, table2 As Range Dim rows1 As Long, rows2 As Long, maxRows As Long Dim i As Long, j As Long Dim v1 As Variant, v2 As Variant Set ws = ActiveSheet Set table1 = ws.Range("A1").CurrentRegion Set table2 = ws.Range("AA1").CurrentRegion If table1.Columns.Count < 12 Or table2.Columns.Count < 12 Then MsgBox "Each table must contain at least 12 columns." Exit Sub End If rows1 = table1.Rows.Count - 1 rows2 = table2.Rows.Count - 1 If rows1 > rows2 Then maxRows = rows1 Else maxRows = rows2 End If Set results = ws.Parent.Worksheets.Add(After:=ws.Parent.Worksheets(ws.Parent.Worksheets.Count)) results.Cells(1, 1).Value = "Data row" For j = 1 To 12 results.Cells(1, j + 1).Value = "Column " & j Next j For i = 1 To maxRows results.Cells(i + 1, 1).Value = i For j = 1 To 12 If i > rows1 Then results.Cells(i + 1, j + 1).Value = "Missing from table 1" ElseIf i > rows2 Then results.Cells(i + 1, j + 1).Value = "Missing from table 2" Else v1 = table1.Cells(i + 1, j).Value2 v2 = table2.Cells(i + 1, j).Value2 If IsError(v1) Or IsError(v2) Then results.Cells(i + 1, j + 1).Value = "Check error" ElseIf StrComp(CStr(v1), CStr(v2), vbBinaryCompare) = 0 Then results.Cells(i + 1, j + 1).Value = "Match" Else results.Cells(i + 1, j + 1).Value = "Different" End If End If Next j Next i End Sub
Return to the source worksheet, choose Developer > Macros, select CompareDynamicTables, and click Run. Review the new sheet for Different, Missing, or Check error results.
Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) to retain the macro. After the input changes, select the source worksheet and run CompareDynamicTables again; each run creates a new results sheet.

Compare Spreadsheet Data with WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making it easy to move existing workbooks. Use formulas for row comparisons; availability of Excel VBA macros may vary by WPS version and platform.

Frequently Asked Questions
How does CurrentRegion find each table?
Range("A1").CurrentRegion expands from A1 through the surrounding contiguous range, stopping at blank rows and columns. Change A1 and AA1 in the macro to the upper-left header cells of your tables.
What happens if the tables have different numbers of rows?
The macro compares up to the longer table's last detected row. It marks each column of an unmatched row as missing from table 1 or table 2.
Can I compare rows by an ID instead of their position?
Not with this example. It compares rows in their existing order. For ID-based matching, modify the macro to find each ID in the other table before comparing the 12 columns.
Why does the macro report fewer rows than expected?
A completely blank row can break a CurrentRegion. Check the source worksheet for blank rows inside each table and remove them, or use a different range-detection method.
Do the results update automatically when the tables change?
No. Select the source worksheet and run Developer > Macros > CompareDynamicTables again to create a fresh results sheet.




