Function Problems

How to Delete Excel Rows That Do Not Match a List of Values

Aamir Naveed AkramAamir Naveed Akram Nov 11, 2026 869 views

Question details

The user needs a way to filter an Excel dataset against an allowed list of values and delete every row where the value is not included in that list.

How to Delete Excel Rows That Do Not Match a List of Values
Product
Excel
Device & OS
not provided
Scenario
Cleaning up a large dataset by discarding records that do not belong to a predefined target list of identifiers.
Observed behavior
The user wants to keep only the rows containing the approved values and permanently delete or filter out the non-matching rows from the worksheet.
Before you start

Always save a backup copy of your workbook before running macros or permanently deleting rows, as these automated actions cannot easily be undone.

Solution 1Recommended

Delete Non-Matching Rows Using a VBA Macro

Use a VBA script to iterate through your data from bottom to top and permanently delete any row that does not contain an approved value from your array.

When you need to permanently remove data from your original worksheet, a VBA macro is the most efficient method. It is critical to loop through the rows from bottom to top; otherwise, deleting a row shifts the remaining rows up, causing the loop to skip records.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications window. Click on 'Insert' in the top menu and select 'Module'.

2
Paste and Customize the Code

Paste the following code into the module window: Sub DeleteOtherRecords() Dim rg As Range, arr As Variant, i As Long Set rg = ActiveSheet.Range("A1").CurrentRegion arr = Array("3340", "3341", "3345", "3347", "S5D1", "S5D7") Application.ScreenUpdating = False For i = rg.Rows.Count To 2 Step -1 If UBound(Filter(arr, rg.Cells(i, 3).Value)) = -1 Then rg.Rows(i).Delete Next i Application.ScreenUpdating = True End Sub Update the 'arr = Array(...)' line with your specific allowed values. Change 'rg.Cells(i, 3)' if the values you are checking are not in the 3rd column (Column C).

3
Run the Macro

Press F5 or click the 'Run' button in the toolbar. The macro will scan your data and delete all rows that do not match the values specified in the array.

Delete Non-Matching Rows Using a VBA Macro
Important: Ensure that your data range starts at cell A1 and has no completely blank rows or columns that might break the 'CurrentRegion' selection.
Efficient Data Management

Manage and Filter Data Efficiently in WPS Spreadsheet

WPS Spreadsheet provides powerful data handling tools, including Advanced Filter and robust formula support, allowing you to easily extract or manage rows that match complex criteria without needing to write code.

  1. 1. Open Your Data: Launch WPS Spreadsheet and open the workbook containing the dataset you need to clean.
  2. 2. Set Up an Advanced Filter: Navigate to the 'Data' tab and click on 'Advanced'. This tool allows you to filter your main dataset based on a separate list of criteria.
  3. 3. Define Your Criteria List: Select your original dataset as the 'List range', and highlight the cells containing your allowed values as the 'Criteria range'. Choose to filter the list in-place or copy the matching results to another location.
  4. 4. Delete Unwanted Rows: If you filtered the list in-place, you can easily copy the visible (matching) rows to a new sheet, and then delete the old sheet entirely.
Free to use with a lightweight installationHigh compatibility with Microsoft Excel formulas and functionsBuilt-in Advanced Filter to easily handle complex list criteriaFamiliar tabbed interface for seamless workflow transitions
microsoft office alternative - wps office

Frequently Asked Questions

Why does the VBA macro loop from bottom to top?

When a row is deleted in Excel, the rows below it automatically shift up. If a macro loops from top to bottom, this shifting causes the macro to skip evaluating the row immediately following a deleted row. Looping backward ensures the row indexes remain accurate throughout the process.

Does the FILTER function permanently delete my data?

No. The FILTER function in Microsoft 365 generates a new, dynamic array of data that meets your criteria while leaving the original dataset completely unchanged.

Can I filter by a list without using macros or formulas?

Yes. You can use the 'Advanced Filter' feature found under the Data tab. By setting up your allowed values in a separate criteria range on your sheet, Advanced Filter can instantly hide rows that do not match your list or extract the matching rows to a new location.