When working with large data sets, you may have the need to quickly delete rows based on the cell values in it (or based on a condition).
For example, consider the following examples:
- You have sales rep data and you want to delete all the records for a specific region or product.
- You want to delete all the records where the sale value is less than 100.
- You want to delete all rows where there is a blank cell.
There are multiple ways to skin this data cat in Excel.
The method you choose to delete the rows will depend on how your data is structured and what’s the cell value or condition based on which you want to delete these rows.
In this tutorial, I will show you multiple ways to delete rows in Excel based on a cell value or a condition.
Filter Rows based on Value/Condition and Then Delete it
One of the fastest ways to delete rows that contain a specific value or fulfill a given condition is to filter these. Once you have the filtered data, you can delete all these rows (while the remaining rows remain intact).
Excel filter is quite versatile and you can filter based on many criteria (such as text, numbers, dates, and colors)
Let’s see two examples where you can filter the rows and delete them.
Delete Rows that contain a specific text
Suppose you have a data set as shown below and you want to delete all the rows where the region is Mid-west (in Column B).
While in this small dataset you can choose to do delete these rows manually, often your datasets are going to be huge where deleting rows manually won’t be an option.
In that case, you can filter all the records where the region is Mid-West and then delete all these rows (while keeping the other rows intact).
Below are the steps to delete rows based on the value (all Mid-West records):
- Select any cell in the data set from which you want to delete the rows
- Click on the Data tab
- In the ‘Sort & Filter’ group, click on the Filter icon. This will apply filters to all the headers cells in the dataset
- Click on the Filter icon in the Region header cell (this is a small downward-pointing triangle icon at the top-right of the cell)
- Deselect all the other options except the Mid-West option (a quick way to do this is by clicking on the Select All option and then clicking on the Mid-West option). This will filter the dataset and only show you records for Mid-West region.
- Select all the filtered records
- Right-click on any of the selected cells and click on ‘Delete Row’
- In the dialog box that opens, click on OK. At this point, you will see no records in the dataset.
- Click the Data tab and click on the Filter icon. This will remove the filter and you will see all the records except the deleted ones.
The above steps first filter the data based on a cell value (or can be other condition such as after/before a date or greater/less than a number). Once you have the records, you simply delete these.
Some useful shortcuts to know to speed up the process:
- Control + Shift + L to apply or remove the filter
- Control + – (hold the control key and press the minus key) to delete the selected cells/rows
In the above example, I had only four distinct regions and I could manually select and deselect it from the Filter list (in steps 5 above).
In case you have a lot of categories/regions, you can type the name in the field right above the box (that has these region names), and Excel will show you only those records that match entered text (as shown below). Once you have the text based on which you want to filter, hit the Enter key.
Note that when you delete a row, anything that you may have in other cells in these rows will be lost. One way to get around this is to create a copy of the data in another worksheet and delete the rows in the copied data. Once done, copy it back in place of the original data.
Or
You can use the methods shown later in this tutorial (using the Sort method or the Find All Method)
Delete Rows Based on a Numeric Condition
Just as I used the filter method to delete all the rows that contain the text Mid-West, you can also use a number condition (or a date condition).
For example, suppose I have the below dataset and I want to delete all the rows where the sale value is less than 200.
Below are the steps to do this:
- Select any cell in the data
- Click on the Data tab
- In the ‘Sort & Filter’ group, click on the Filter icon. This will apply filters to all the headers cells in the dataset
- Click on the Filter icon in the Sales header cell (this is a small downward-pointing triangle icon at the top-right of the cell)
- Hover the cursor over the Number Filters option. This will show you all the number related filter options in Excel.
- Click on the ‘Less than’ option.
- In the ‘Custom Autofilter’ dialog box that opens, enter the value ‘200’ in the field
- Click OK. This will filter and show only those records where the sales value is less than 200
- Select all the filtered records
- Right-click on any of the cells and click on Delete Row
- In the dialog box that opens, click on OK. At this point, you will see no records in the dataset.
- Click the Data tab and click on the Filter icon. This will remove the filter and you will see all the records except the deleted ones.
There are many number filters that you can use in Excel – such as less than/greater than, equal/does not equal, between, top 10, above or below average, etc.
Note: You can use multiple filters as well. For example, you can delete all the rows where the sales value is greater than 200 but less than 500. In this case, you need to use two filter conditions. The Custom Autofilter dialog box allows having two filter criteria (AND as well as OR).
Just like the number filters, you can also filter the records based on the date. For example, if you want to remove all the records of the first quarter, you can do that by using the same steps above. When you’re working with Date filters, Excel automatically shows you relevant filters (as shown below).
While filtering is a great way to quickly delete rows based on a value or a condition, it has one drawback – it deletes the entire row. For example, in the below case, it would delete all the data which is to the right of the filtered dataset.
What if I only want to delete records from the dataset, but want to keep the remaining data intact.
You can’t do that with filtering, but you can do that with sorting.
Sort the Dataset and Then Delete the Rows
Although sorting is another way to delete rows based on value, but in most cases, you’re better off using the filter method covered above.
This sorting technique is recommended only when you want to delete the cells with the values and not the entire rows.
Suppose you have a dataset as shown below and you want to delete all the records where the region is Mid-west.
Below are the steps to do this using sorting:
- Select any cell in the data
- Click on the Data tab
- In the Sort & Filter group, click on the Sort icon.
- In the Sort dialog box that opens, select Region in the sort by column.
- In the Sort on option, make sure Cell Values is selected
- In Order option, select A to Z (or Z to A, doesn’t really matter).
- Click OK. This will give you the sorted data set as shown below (sorted by column B).
- Select all the records with the region Mid-West (all the cells in the rows, not just the region column)
- Once selected, right-click and then click on Delete. This will open the Delete dialog box.
- Make sure the ‘Shift cells up’ option is selected.
- Click OK.
The above steps would delete all the records where the region was Mid-West, but it doesn’t delete the entire row. So, if you have any data on the right or left of your dataset, it will remain unharmed.
In the above example, I have sorted the data based on the cell value, but you can also use the same steps to sort based on numbers, dates, cell color or font color, etc.
Here is a detailed guide on how to sort data in Excel
In case you want to keep the original data set order but remove the records based on criteria, you need to have a way to sort the data back to the original one. To do this, add a column with serial numbers before sorting the data. Once you’re done with deleting the rows/records, simply sort based using this extra column you added.
Find and Select the Cells Based on Cell Value and Then Delete the Rows
Excel has a Find and Replace functionality that can be great when you want to find and select cells with a specific value.
Once you have selected these cells, you can easily delete the rows.
Suppose you have the dataset as shown below and you want to delete all the rows where the region is Mid-West.
Below are the steps to do this:
- Select the entire dataset
- Click the Home tab
- In the Editing group, click on the ‘Find & Select’ option and then click on Find (you can also use the keyboard shortcut Control + F).
- In the Find and Replace dialog box, enter the text ‘Mid-West’ in the ‘Find what:’ field.
- Click on Find All. This will instantly show you all the instances of the text Mid-West that Excel was able to find.
- Use the keyboard shortcut Control + A to select all the cells that Excel found. You will also be able to see all the selected cells in the dataset.
- Right-click on any of the selected cells and click on Delete. This will open the Delete dialog box.
- Select the ‘Entire row’ option
- Click OK.
The above steps would delete all the cells where the region value is Mid-west.
Note: Since Find and Replace can handle wild card characters, you can use these when finding data in Excel. For example, if you want to delete all the rows where the region is either Mid-West or South-West, you can use ‘*West‘ as the text to find in the Find and Replace dialog box. This will give you all the cells where the text ends with the word West.
Delete All Rows With a Blank Cell
In case you want to delete all the rows where there are blank cells, you can easily do this with an inbuilt functionality in Excel.
It’s the Go-To Special Cells option – which allows you to quickly select all the blank cells. And once you have selected all the blank cells, deleting these is super simple.
Suppose you have the dataset as shown below and I want to delete all the rows where I don’t have the sale value.
Below are the steps to do this:
- Select the entire dataset (A1:D16 in this case).
- Press the F5 key. This will open the ‘Go To’ dialog box (You can also get this dialog box from Home –> Editing –> Find and Select –> Go To).
- In the ‘Go To’ dialog box, click on the Special button. This will open the ‘Go To Special’ dialog box
- In the Go To Special dialog box, select ‘Blanks’.
- Click OK.
The above steps would select all the cells that are blank in the dataset.
Once you have the blank cells selected, right-click on any of the cells and click on Delete.
In the Delete dialog box, select the ‘Entire row’ option and click OK. This will delete all rows that have blank cells in it.
If you’re interested in learning more about this technique, I wrote a detailed tutorial on how to delete rows with blank cells. It includes the ‘Go To Special’ method as well as a VBA method to delete rows with blank cells.
Filter and Delete Rows Based On Cell Value (using VBA)
The last method that I am going to show you include a little bit of VBA.
You can use this method if you often need to delete rows based on a specific value in a column. You can add the VBA code once and add it your Personal Macro workbook. This way it will be available for use in all of your Excel workbooks.
This code works the same way as the Filter method covered above (except the fact that this does all the steps in the backend and save you some clicks).
Suppose you have the dataset as shown below and you want to delete all the rows where the region is Mid-West.
Below is the VBA code that will do this.
Sub DeleteRowsWithSpecificText() 'Source:https://trumpexcel.com/delete-rows-based-on-cell-value/ ActiveCell.AutoFilter Field:=2, Criteria1:="Mid-West" ActiveSheet.AutoFilter.Range.Offset(1, 0).Rows.SpecialCells(xlCellTypeVisible).Delete End Sub
The above code uses the VBA Autofilter method to first filter the rows based on the specified criteria (which is ‘Mid-West’), then select all the filtered rows and delete it.
Note that I have used Offset in the above code to make sure my header row is not deleted.
The above code doesn’t work if your data is in an Excel Table. The reason for this is that Excel considers an Excel Table as a list object. So if you want to delete rows that are in a Table, you need to modify the code a bit (covered later in this tutorial).
Before deleting the rows, it will show you a prompt as shown below. I find this useful as it allows me to double-check the filtered row before deleting.
Remember that when you delete rows using VBA, you can’t undo this change. So use this only when you’re sure that this works the way you want. Also, it’s a good idea to keep a backup copy of the data just in case anything goes wrong.
In case your data is in an Excel Table, use the below code to delete rows with a specific value in it:
Sub DeleteRowsinTables() 'Source:https://trumpexcel.com/delete-rows-based-on-cell-value/ Dim Tbl As ListObject Set Tbl = ActiveSheet.ListObjects(1) ActiveCell.AutoFilter Field:=2, Criteria1:="Mid-West" Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete End Sub
Since VBA considers Excel Table as a List object (and not a range), I had to change the code accordingly.
Where to Put the VBA code?
This code needs to be placed in the VB Editor backend in a module.
Below are the steps that will show you how to do this:
- Open the workbook in which you want to add this code.
- Use the keyboard shortcut ALT + F11 to open the VBA Editor window.
- In this VBA Editor window, on the left, there is a ‘Project Explorer’ pane (which lists all the workbooks and worksheets objects). Right-click on any object in the workbook (in which you want this code to work), hover the cursor over ‘Insert’ and then click on ‘Module’. This will add the Module object to the Workbook and also open the Module code window on the right
- In the module window (that will appear on the right), copy and paste the above code.
Once you have the code in the VB Editor, you can run the code by using any of the below methods (make sure you have the selected any cell in the dataset on which you want to run this code):
- Select any line within the code and hit the F5 key.
- Click on the Run button in the toolbar in the VB Editor
- Assign the macro to a button or a shape and run it by clicking on it in the worksheet itself
- Add it to the Quick Access Toolbar and run the code with a single click.
You can read all about how to run the macro code in Excel in this article.
Note: Since the workbook contains a VBA macro code, you need to save it in the macro-enabled format (xlsm).
You may also like the following Excel tutorials:
- How to Delete Every Other Row in Excel
- How to Delete a Pivot Table in Excel
- How to Delete Every Other Row in Excel
- How to Move Rows and Columns in Excel (The Best and Fastest Way)
- Highlight Rows Based on a Cell Value in Excel (Conditional Formatting)
- How to Insert Multiple Rows in Excel
- Number Rows in Excel
- How to Quickly Unhide COLUMNS in Excel
- Hide Zero Values in Excel
I have a worksheet, I need to delete rows based on cell value ..
Cells to check are in Column A ..
If cell contains «-» .. Delete Row
I can’t find a way to do this .. I open a workbook, copy all contents to another workbook, then delete entire rows and columns, but there are specific rows that has to be removed based on cell value.
Need Help Here.
UPDATE
Sample of Data I have
asked Jan 30, 2013 at 20:56
sikassikas
5,43728 gold badges74 silver badges120 bronze badges
4
The easiest way to do this would be to use a filter.
You can either filter for any cells in column A that don’t have a «-» and copy / paste, or (my more preferred method) filter for all cells that do have a «-» and then select all and delete — Once you remove the filter, you’re left with what you need.
Hope this helps.
answered Jan 30, 2013 at 21:17
John BustosJohn Bustos
18.9k16 gold badges87 silver badges148 bronze badges
2
The screenshot was very helpful — the following code will do the job (assuming data is located in column A starting A1):
Sub RemoveRows()
Dim i As Long
i = 1
Do While i <= ThisWorkbook.ActiveSheet.Range("A1").CurrentRegion.Rows.Count
If InStr(1, ThisWorkbook.ActiveSheet.Cells(i, 1).Text, "-", vbTextCompare) > 0 Then
ThisWorkbook.ActiveSheet.Cells(i, 1).EntireRow.Delete
Else
i = i + 1
End If
Loop
End Sub
Sample file is shared: https://www.dropbox.com/s/2vhq6vw7ov7ssya/RemoweDashRows.xlsm
answered Jan 30, 2013 at 23:02
Peter L.Peter L.
7,2465 gold badges34 silver badges53 bronze badges
6
You could copy down a formula like the following in a new column…
=IF(ISNUMBER(FIND("-",A1)),1,0)
… then sort on that column, highlight all the rows where the value is 1 and delete them.
answered Jan 30, 2013 at 21:31
weirweir
4,4722 gold badges28 silver badges42 bronze badges
0
if you want to delete rows based on some specific cell value.
let suppose we have a file containing 10000 rows, and a fields having value of NULL.
and based on that null value want to delete all those rows and records.
here are some simple tip.
First open up Find Replace dialog, and on Replace tab, make all those cell containing NULL values with Blank.
then press F5 and select the Blank option, now right click on the active sheet, and select delete, then option for Entire row.
it will delete all those rows based on cell value of containing word NULL.
answered Feb 15, 2014 at 18:03
zaheerzaheer
791 silver badge1 bronze badge
If you’re file isn’t too big you can always sort by the column that has the — and once they’re all together just highlight and delete. Then re-sort back to what you want.
answered Dec 1, 2016 at 23:56
1
You can loop through each the cells in your range and use the InStr
function to check if a cell contains a string, in your case; a hyphen.
Sub DeleteRowsWithHyphen()
Dim rng As Range
For Each rng In Range("A2:A10") 'Range of values to loop through
If InStr(1, rng.Value, "-") > 0 Then 'InStr returns an integer of the position, if above 0 - It contains the string
rng.Delete
End If
Next rng
End Sub
answered Jan 30, 2013 at 22:58
Francis DeanFrancis Dean
2,3862 gold badges21 silver badges29 bronze badges
This is the autofilter macro you could base a function off of:
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10").AutoFilter Field:=1, Criteria1:="=*-*", Operator:=xlAnd
Selection.AutoFilter
I use this autofilter function to delete matching rows:
Public Sub FindDelete(sCol As String, vSearch As Variant)
'Simple find and Delete
Dim lLastRow As Integer
Dim rng As Range
Dim rngDelete As Range
Range(sCol & 1).Select
[2:2].Insert
Range(sCol & 2) = "temp"
With ActiveSheet
.usedrange
lLastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
Set rng = Range(sCol & 2, Cells(lLastRow, sCol))
rng.AutoFilter Field:=1, Criteria1:=vSearch, Operator:=xlAnd
Set rngDelete = rng.SpecialCells(xlCellTypeVisible)
rng.AutoFilter
rngDelete.EntireRow.Delete
.usedrange
End With
End Sub
call it like:
call FindDelete "A", "=*-*"
It’s saved me a lot of work. Good luck!
answered Jun 3, 2014 at 18:03
1
This post will guide you how to use the Find & Replace feature to delete or remove all rows based on certain cell value in Microsoft Excel. Or how to delete all rows that contain certain value with VBA code in excel, such as, removing all rows if cell contains 0 or any other value.
Assuming that you have a worksheet and you want to delete rows based on cell vlaue if the value is equal to 0. In another word, if the cell value contain 0, then delete that row contain value 0. The first thing you need to do is that how to find rows that contain certain value in your worksheet. And how to solve this problem? You can use the Find & Replace command or VBA code to achieve the result.
Delete rows based on cell value with Find & Replace feature
To delete rows based on a certain cell value with Find & Replace feature, you can refer to the following steps:
1# Select the range of cells that you want to delete rows based on certain cell value.
2# on the HOME tab, click Find & Replace command under Editing group. Or just press Ctrl +F shortcut to open the Find and Replace box.
3# the Find and Replace dialog box will appear on the screen.
4# type the certain value in the Find what: text box, and then click Find All button
5# you will see that the searched result will appear in the Find and Replace window.
6# select all found values in the Find and Replace window and you will see that all cells contain certain value will be highlighted in your selected range.
7# right-click on the selected cells, and select Delete…menu from the drop-down menu list. Then choose Entire row radio button in the Delete dialog box. Click OK button.
Or you can go to HOME tab, click Delete command. Then click Delete Sheet Rows.
So far, all rows contain the certain value are deleted in your selected range. And if you want to remove the columns base on the certain value, just select Entire column radio button in the step 7.
Delete Rows Based on cell value with Excel VBA Macro (VBA code)
If you want to delete rows that contain the certain value in your worksheet with excel VBA Marco, you can refer to the following steps:
1# click on “Visual Basic” command under DEVELOPER Tab.
2# then the “Visual Basic Editor” window will appear.
3# click “Insert” ->”Module” to create a new module
4# paste the below VBA code into the code window. Then clicking “Save” button.
Sub RemoveRowsBasedValue() Dim Dr As Range Set Ip = Application.Selection Set Ip = Application.InputBox("Select one range that you want to remove rows:", "RemoveRowsBasedValue", Ip.Address, Type:=8) Ds = Application.InputBox("Please type a text string:", "RemoveRowsBasedValue", Type:=2) For Each R In Ip If R.Value = Ds Then If Dr Is Nothing Then Set Dr = R Else Set Dr = Application.Union(Dr, R) End If End If Next Dr.EntireRow.Delete End Sub
5# back to the current worksheet, then run the above excel macro. Click Run button.
6# select a range that you want to delete rows, click OK button. Then type a text string contained in the range of rows to delete.
See all How-To Articles
This tutorial demonstrates how to delete rows if a cell contains specific text in Excel.
Delete Rows With Specific Text
Say you have data set with names in Columns A, B, and C and want to select all cells with the name John, then delete all rows containing those cells.
- First, select the data set (A2:C6). Then in the Ribbon, go to Home > Find & Select > Find.
- The Find and Replace dialog window opens. In Find what box, type the value you are searching for (here, John), then click Find All.
You can also find and replace with VBA.
- The results are listed at the bottom of the Find and Replace window. Select all of them and click Close.
As a result, all cells with the specific value (John) are selected.
- To delete rows that contain these cells, right-click anywhere in the data range and from the drop-down menu, choose Delete.
- In the Delete dialog window, choose the Entire row and click OK.
As a result, all the rows with cells that contain specific text (here, John) are deleted.
Note: You can also use VBA code to delete entire rows.
In this guide, we’re going to show you how to remove all rows containing specific value in Excel. We are going to be taking a look at two methods:
- Find and Replace
- Filter
These methods are good for removing all rows containing a specific value, such as repeating headers.
Removing all rows containing specific value using Find and Replace
- If you wish to perform removing a specific range, select your data, including the values you want to remove. Otherwise, you do not need to select any cells.
- Press the Ctrl + F key combination to open Find and Replace.
- Type in the value in the rows you want to remove.
- Click Find All.
- Select any of the found values in the results pane.
- Press Ctrl + A to select all of them. You can select specific values you want to remove by using Ctrl or Shift keys.
- Close the Find and Replace window.
- Press Ctrl + — to open Delete
- Select Entire row
- Click OK button to delete those rows.
After clicking OK, Excel will remove all rows containing the selected cells.
Warning: If found and selected items contain an Excel Table header, the Ctrl + — shortcut will not work.
Removing all rows containing specific value using Filter
This approach is more suitable for data in a table structure.
- Select your data, including the values you want to remove.
- Press Ctrl + Shift + L to enable Filters. Alternatively, you can click Filter in Data tab.
- Click the down arrow in the header of the column that contains the value you want to select.
- Select the value(s) you want to remove in the filter dialog.
- Click OK to apply the filter.
- Select each cell individually by clicking cells while holding down the Ctrl key. Don’t use the Shift key or drag the mouse to select cells.
- Once the cells are selected, press Ctrl + —.
- Click OK on prompt dialog to delete rows.
Clear the filters to see the remaining rows.