How to Automatically Delete Excel Rows Based on Multiple Conditions
Question details
The user needs a method to automatically remove specific rows in a spreadsheet when criteria across multiple columns are met simultaneously.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Managing a dataset of approximately 1,000 rows where manual deletion is inefficient, requiring a macro to delete rows when column F contains one specific value and column H contains one of two other specific values.
- Observed behavior
- The user requires an automated script that can safely process the entire dataset without skipping rows during the deletion process.
Always create a backup copy of your workbook before running any VBA macros, as row deletions executed via VBA cannot be undone using the standard Undo feature.
Use a VBA Macro to Delete Rows Using a Bottom-Up Loop
This automated method uses a VBA script to evaluate your conditions and deletes matching rows. It loops from the bottom up to ensure no rows are skipped when the spreadsheet shifts.
When deleting rows programmatically, it is crucial to loop backward from the last row to the first. If you loop forward, deleting a row shifts all subsequent rows up, causing the macro to skip evaluating the row that immediately follows the deleted one.
Open your Excel workbook and press Alt + F11 on your keyboard to launch the Microsoft Visual Basic for Applications (VBA) editor.
In the top menu bar of the VBA editor, click 'Insert' and select 'Module' from the dropdown to create a blank script window.
Copy and paste the following macro code into the module window. Ensure you replace 'Sheet1', 'N1', 'N2', and 'N3' with your actual sheet name and target values: Sub RemoveRowsBasedOnConditions() Dim ws As Worksheet Dim i As Long Set ws = ThisWorkbook.Sheets("Sheet1") For i = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row To 2 Step -1 If ws.Cells(i, "F").Value = "N1" And _ (ws.Cells(i, "H").Value = "N2" Or ws.Cells(i, "H").Value = "N3") Then ws.Rows(i).Delete End If Next i End Sub
Close the VBA editor to return to your worksheet. Press Alt + F8, select 'RemoveRowsBasedOnConditions' from the Macro dialog box, and click 'Run'.

Use WPS Office to Run Macros and Clean Data Efficiently
WPS Spreadsheet offers excellent built-in support for VBA and macros, allowing you to automate repetitive tasks like deleting rows based on complex conditions. It provides a familiar interface and allows you to execute your data-cleaning scripts effortlessly.
- 1. Open Your File in WPS Spreadsheet: Launch WPS Office and open your workbook containing the dataset.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click on 'VBA Editor', or simply use the Alt + F11 shortcut.
- 3. Run the Deletion Script: Insert a new module, paste your VBA row-deletion code, and run the macro to instantly filter and clean your data.

Frequently Asked Questions
Why do I need to loop backwards when deleting rows in Excel using VBA?
When a row is deleted, Excel immediately shifts all the rows below it upward to fill the gap. If your macro loops forward (e.g., from row 2 to 100), deleting row 5 shifts row 6 into the 5th position. The macro then moves to evaluate row 6, completely skipping the data that shifted up. Looping backwards (from 100 to 2) prevents this issue entirely.
Can I use this VBA code for a different worksheet?
Yes. In the provided VBA code, locate the line that says `Set ws = ThisWorkbook.Sheets("Sheet1")`. Change `"Sheet1"` to the exact name of your target worksheet, such as `"Data"` or `"Report"`.
How do I change the columns the macro checks?
In the macro code, look for `ws.Cells(i, "F")` and `ws.Cells(i, "H")`. Change the letters "F" and "H" to whichever column letters you wish to evaluate for your specific conditions.
Does WPS Office support the same VBA macros as Microsoft Excel?
Yes, WPS Office provides excellent VBA support. You can copy and paste most standard Excel macros, including row deletion and data manipulation scripts, directly into the WPS VBA editor and run them seamlessly.




