VBA & Macro Problems

Delete the Last Output Row by Cell Values in Excel VBA

Anum MushtaqAnum Mushtaq Dec 2, 2026 868 views

Question details

Delete an unwanted final output row when its values in columns E and R match known identifiers.

Delete the Last Output Row by Cell Values in Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A VBA-generated workbook has an unwanted final row whose row number varies between files.
Observed behavior
The final row cannot be identified by a fixed row number, but it can be recognized by values in columns E and R.
Before you start

Identify the output worksheet and the exact values that mark the unwanted row in columns E and R. Confirm that column E is populated in the final output row and that row 1 is a header, or adjust the code accordingly.

Solution 1Recommended

Check and delete the last output row with Excel VBA

Use this approach after generating the output when column E reliably identifies its last populated row.

The procedure finds the last nonblank cell in column E and deletes its row only if both identifiers match. It leaves the row unchanged if either value differs.

1
Open the VBA editor

In Excel, open the Developer tab and select Visual Basic. In the VBA editor, select the workbook's project and choose Insert > Module.

2
Add the deletion procedure

Paste this procedure into the module: Sub DeleteUnwantedLastRow() Dim ws As Worksheet Dim lastRow As Long Dim expectedE As String Dim expectedR As String Set ws = ThisWorkbook.Worksheets("Output") expectedE = "value in E" expectedR = "value in R" lastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row If lastRow > 1 Then If ws.Cells(lastRow, "E").Value = expectedE And _ ws.Cells(lastRow, "R").Value = expectedR Then ws.Rows(lastRow).Delete End If End If End Sub

3
Set the worksheet and identifiers

Replace "Output", "value in E", and "value in R" in the module with the actual worksheet name and identifying values. If the output is in a different workbook, change the ThisWorkbook reference to that workbook.

4
Run the check after output generation

Add DeleteUnwantedLastRow at the end of your output-generation macro, after all rows have been written. Run the macro and inspect the output worksheet to confirm that only the matching final row is removed.

Check and delete the last output row with Excel VBA
Check the last-row column: If the final output row can have a blank cell in column E, use a column that is always populated to find lastRow. Keep the checks against columns E and R if those are still the identifiers.
Free Microsoft Office alternative

Work with Excel output files in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and continue working with your data when migrating between Office applications. VBA support varies by version, so verify macros separately.

Review and edit spreadsheet output rows in WPS SpreadsheetOpen and save compatible Microsoft Excel formats such as XLSXUse a familiar spreadsheet interface for everyday tasksMove compatible documents between WPS Office and Microsoft Office
microsoft office alternative - wps office

Frequently Asked Questions

What happens if only one of the two values matches?

The row is not deleted. The VBA If statement uses And, so both the column E and column R values must match.

Why does the code use column E to find the last row?

The expression ws.Cells(ws.Rows.Count, "E").End(xlUp).Row finds the last nonblank cell in column E. If E can be blank in the final output row, replace "E" in that expression with a reliably populated column.

How do I target a workbook created by the macro?

Replace ThisWorkbook in the Set ws line with a reference to the generated workbook, such as an existing workbook variable, and specify its output worksheet name.

Where should I put the deletion check?

In the VBA editor, add the procedure to a module and call DeleteUnwantedLastRow after the output-generation code has finished writing rows.