Delete the Last Output Row by Cell Values in Excel VBA
Question details
Delete an unwanted final output row when its values in columns E and R match known identifiers.

- 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.
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.
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.
In Excel, open the Developer tab and select Visual Basic. In the VBA editor, select the workbook's project and choose Insert > Module.
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
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.
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.

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.

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.




