How to Delete Row If Cell Contains Specific Values in Excel?

We will be using a sample dataset that contains the Name and Age of some people.

excel delete row if cell contains specific values dataset


Method 1 – Using Find and Replace to Delete a Row If a Cell Contains Specific Text in Excel

We want to find and delete rows that have cells starting with “Mr.” in the Name column.

Steps:

  • Press Ctrl + F to open with Find and Replace window.
  • Type “Mr.” within the Find what bar.
  • Click on the Find All option.
  • Select one of the found results and then press Ctrl + A to select all the found results.
  • Hit the Close option.

Use of Find and Replace to Delete Row If Cell Contains Specific Text in Excel

  • Press the Ctrl + – buttons to open the Delete dialog box.
  • Select the Shift cells up option and hit OK.

Use of Find and Replace

  • The output will appear as below.

Use of Find and Replace option


Method 2 – Utilizing AutoFilter to Remove an Excel Row If a Cell Contains a Certain Value

We will delete all the Excel records starting with “Ms. Liesel” in the Name column.

Steps: 

  • Select the entire data table.
  • Go to Data, then choose Sort & Filter, and select Filter.

Use of AutoFilter to Remove an Excel Row If Cell Contains Certain Value

  • Click on the drop-down icon at the corner in the Name column.
  • Go to Text Filters and choose the Begins With option.

Use of AutoFilter Feature in Excel

  • A dialog box named Custom AutoFilter will pop up on the screen.
  • Type Ms. Liesel in the begins with bar and hit OK.

Use of AutoFilter Feature in Excel

  • Press the Ctrl + – buttons and a dialog box will appear.
  • Hit the OK button.

Use of AutoFilter Feature in Excel

  • The output will look like the following image.

Use of AutoFilter Feature in Excel


Method 3 – Deleting a Row If a Cell Contains Specific Values in Excel Using VBA Code

Case 3.1 – Delete a Row If a Cell Contains Specific Text

Steps:

  • Press Alt + F11 to open the VBA window.
  • Go to Insert and select Module to open a new module.

Delete Row If Cell Contains Specific Values in Excel Using VBA Code

  • Copy the following code:
Sub DeleteRowsContainingtext()
Dim A As Worksheet
Dim B As Integer
Dim Step As Long
Set A = Worksheets("VBA")
For B = A.Range("B5:C14").Rows.Count To 1 Step -1
If Application.WorksheetFunction.IsText(Cells(B + 2, 2)) = True Then
A.Cells(B + 2, 2).EntireRow.Delete
End If
Next
End Sub
  • Paste it in the VBA editor and save it by pressing Ctrl + S.

Delete Row If Cell Contains Specific Text with VBA

  • Hit the F5 key to run the code and you will get your desired output.

Use of VBA


Case 3.2 – Delete a Row If a Cell Contains Number

We will delete any row if a cell in the column Age contains 17.

Steps:

  • Press Alt +F11 to open the VBA window.
  • Go to Insert and chopse Module to open a new module.
  • Copy the following code:
Sub DeleteRowsContainingNumbers()
Dim A As Long
Dim B As Long
A = 1000
For B = A To 1 Step -1
If Cells(B, 3).Value = "17" Then
Rows(B).Delete
End If
Next
End Sub
  • Paste it in the VBA editor and save it by pressing Ctrl + S.

Delete Row If Cell Contains Specific Number with VBA

  • Hit the F5 key to run the code and you will get your desired output.

Use of VBA


Things to Remember

  • Press Ctrl + F to open the Find and Replace dialog box.
  • Use Ctrl + – to delete cells or rows.
  • Press Alt + F11 to open the VBA window.

Download the Practice Workbook


You May Also Like These


<< Go Back to Delete Rows | Rows in Excel | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Mrinmoy Roy
Mrinmoy Roy

Mrinmoy Roy, a dedicated professional with a BSc in Electronics and Communication Engineering from Khulna University of Engineering & Technology, Bangladesh, brings over two years of expertise to the ExcelDemy project. As a prolific contributor, he has authored around 180 articles, showcasing his deep knowledge and passion for Microsoft Excel, Data Analysis, and VBA. His unwavering commitment to continuous learning, combined with versatile skills, renders him well-suited for roles in data management and spreadsheet solutions. He has interest... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo