VBA & Macro Problems

Add a Table Row and Archive Data with Excel VBA

Amos GikundaAmos Gikunda Nov 18, 2026 869 views

Question details

Add a row to an Excel destination table and write values from a changed source row into it.

Add a Table Row and Archive Data with Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A worksheet change to a status column should archive the source row in another table when the status is Yes.
Observed behavior
The existing macro attempts to use PasteSpecial on a ListObject instead of writing to a range in a newly added table row.
Before you start

Confirm that the source table is named Table1, its status field is column 21 of the table, and the destination table CompletedOrders is on the Completed RCD Orders worksheet. The destination table must have at least 36 columns in the same order as source columns A:AJ.

Solution 1Recommended

Add a ListRow and assign the source values

Use this worksheet event to archive a row when a single status cell changes to Yes.

ListRows.Add returns a table row whose Range can receive values. Direct assignment copies values rather than formulas or formatting.

This example deletes the source table row after the destination write succeeds. Cells outside the source table are not deleted, even if they fall within A:AJ.

1
Open the worksheet code

In Excel, press Alt+F11, then open the source worksheet's code module in the Project pane. Replace any existing Worksheet_Change procedure there with the following code.

2
Add the event procedure

Paste this code into the worksheet module: Private Sub Worksheet_Change(ByVal Target As Range) Dim tbl As ListObject Dim tblCO As ListObject Dim newRow As ListRow Dim src As Range Dim rowIndex As Long Dim oldEvents As Boolean Dim oldScreenUpdating As Boolean Dim errorMessage As String If Target.CountLarge <> 1 Then Exit Sub oldEvents = Application.EnableEvents oldScreenUpdating = Application.ScreenUpdating On Error GoTo CleanFail Set tbl = Me.ListObjects("Table1") If tbl.DataBodyRange Is Nothing Then Exit Sub If Intersect(Target, tbl.DataBodyRange.Columns(21)) Is Nothing Then Exit Sub If IsError(Target.Value2) Then Exit Sub If LCase$(Trim$(CStr(Target.Value2))) <> "yes" Then Exit Sub Set src = Me.Range("A" & Target.Row & ":AJ" & Target.Row) Set tblCO = ThisWorkbook.Worksheets("Completed RCD Orders").ListObjects("CompletedOrders") If tblCO.ListColumns.Count < src.Columns.Count Then MsgBox "The destination table needs at least 36 columns.", vbExclamation Exit Sub End If rowIndex = Target.Row - tbl.DataBodyRange.Row + 1 Application.EnableEvents = False Application.ScreenUpdating = False Set newRow = tblCO.ListRows.Add newRow.Range.Cells(1, 1).Resize(1, src.Columns.Count).Value = src.Value tbl.ListRows(rowIndex).Delete CleanUp: Application.EnableEvents = oldEvents Application.ScreenUpdating = oldScreenUpdating If Len(errorMessage) > 0 Then MsgBox errorMessage, vbExclamation Exit Sub CleanFail: errorMessage = "Archive failed: " & Err.Description Resume CleanUp End Sub

3
Save and test

Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). In Table1, change one cell in the 21st table column to Yes, then check that CompletedOrders contains the values and the source table row has been removed.

Add a ListRow and assign the source values
Check a partial archive: If an error occurs after the destination row is added, the new row may remain even though the source row was not deleted. Check both tables before retrying.
Free Microsoft Office alternative

Work with spreadsheet data in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for working with Office files and moving everyday spreadsheet tasks to WPS. It does not fix Excel's VBA event handling, and macro compatibility can vary.

Review and copy spreadsheet rows using a familiar interface.Open and edit compatible Microsoft Excel formats such as XLSX and XLS.Use a free, lightweight Office alternative for everyday spreadsheet work.
microsoft office alternative - wps office

Frequently Asked Questions

Why can't I paste directly into a ListObject?

A ListObject represents the table, not a single-cell paste destination. Add a row with ListRows.Add and write to newRow.Range, or use a cell in that range as the paste destination.

Does the macro copy formulas and formatting?

No. The Range.Value assignment writes values only. Formulas from A:AJ become their current results, and source formatting is not copied.

Why does the code disable events?

Deleting the source table row can trigger Worksheet_Change again. Application.EnableEvents = False prevents recursion, and the cleanup section restores its previous setting.

What happens if I paste Yes into several status cells at once?

This example exits when Target contains more than one cell. Processing a multi-cell paste requires a separate loop that handles each affected table row.

How do I change which column triggers the archive?

Change the 21 in tbl.DataBodyRange.Columns(21) to the status column's position within Table1. This position is relative to the table, not the worksheet.