How to Delete Row If Cell Contains Value Using Macro in Excel

In this article, we will show you how to delete a row if a cell contains a value in Excel utilizing VBA macro. Sometimes we need to remove certain data from a huge dataset in Excel. When the cells in the dataset are carrying values, then we should be really careful before deleting them. Implementing VBA macro is the most effective, quickest, and safest method to run any operation in Excel. Here, we will delete cells that contains either a string or numbers as the following gif.

Demonstrating a Way to Delete Row If a Cell Contains Value by Using Macros in Excel


Download Practice Template

You can download the free practice Excel template from here and practice on your own.


2 Methods in Implementing VBA Macro to Delete Row if Cell Contains Value in Excel

In this section, we will show you how to delete rows containing values from the Excel worksheet using VBA Macro. But before that, let us understand the syntax first.

The VBA syntax to delete rows based on cell values from the Excel worksheet,

If Then Rows(“[Row_Numbers]”).EntireRow.Delete

Here,

Row_Numbers refers to the row numbers to delete,

Delete method as an object of Rows and

EntireRow.Delete defines as a method that will delete the entire row from the dataset.

And in the next section, we will show you how to delete rows if the cells are containing text value or number value in Excel by implementing VBA macro.


1. Embed VBA Macro to Delete Row if Cell Contains String Value in Excel

Consider the following dataset of rows, where there is a list of names. From that name list, we want to delete the name “Ambrose” using the VBA macro in Excel.

Sample Dataset to Delete Row If Cell Contains String Value by Using a Macro in Excel

The steps to delete “Ambrose” from that list by VBA are given below,

Steps:

  • Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

Opening Visual Basic Window to Delete Row If Cell Contains String Value by Using a Macro in Excel

  • In the pop-up code window, from the menu bar, click Insert -> Module.

Inserting Coding Module to Delete Row If Cell Contains String Value by Using a Macro in Excel

  • Copy the following code and paste it into the code window.
Sub Delete_Row_If_Cell_Contains_String()
Dim Row As Long
Dim Wrk As Long
Row = 15
For Wrk = Row To 1 Step -1
    If Cells(Wrk, 2) = "Ambrose" Then
        Rows(Wrk).Delete
    End If
Next
End Sub

Writing VBA Code to Delete Row If Cell Contains String Value by Using a Macro in Excel

Your code is now ready to run.

  • Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Play icon in the sub-menu bar to run the macro.

Running VBA Code to Delete Row If Cell Contains String Value by Using a Macro in Excel

You will notice that the name “Amber” has been deleted from the list.

Delete Row If Cell Contains String Value by Using a Macro in Excel

VBA Code Explanations to Delete a Specific Row from Excel

  • Sub Delete_Row_If_Cell_Contains_String() -> is for starting program and sub procedure to write the VBA code.
  • Dim Row As Long -> Declaring the variable Row as long data type to store the last row number.
  • Dim Wrk As Long -> Declaring the variable Wrk as long data type to initiate in the For Loop.
    • Long is a numerical data type in VBA The long data type in Excel VBA can hold the values from 0 to 2, 147, 483, 647 for positive numbers, and for the negative number it can hold from 0 to -2, 147, 483, 648.
  • Row = 15 -> Storing the last row value to the declared Row variable (we stored 15 rows in our worksheet).
  • For Wrk = Row To 1 Step -1 -> To run the For Loop.
  • If Cells(Wrk, 2) = “Ambrose” Then -> Checking each cell whether the value of the cell is equal to “Rock” (you can put any string according to your need).
  • Rows(Wrk).Delete -> If the above If Statement is true, then delete the row.
  • End If -> Ending the Statement.
  • Next -> Closing the Loop.
  • End Sub -> Ending the Function.

Read more: Delete Multiple Rows in Excel with VBA


Similar Readings


2. Insert VBA Macro to Delete Row if Cell Contains Number Value in Excel

Consider the following dataset of rows, where there is a list consisting of text and number values. From that list, we wanted to delete all the rows containing numbers using VBA macro in Excel.

Sample Dataset to Delete Row If Cell Contains Numeric Value by Using a Macro in Excel

The steps to delete all the rows containing numbers from that list by VBA are given below,

Steps:

  • Press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

Going to Visual Basic Window

  • In the pop-up code window, from the menu bar, click Insert -> Module.

Entering Coding Module

  • Copy the following code and paste it into the code window.
Sub Delete_Row_If_Cell_Contains_Number()
Dim Row As Long
Dim Wrk As Long
Row = 15
For Wrk = Row To 5 Step -1
    If IsNumeric(Cells(Wrk, 2)) Then
        Rows(Wrk).Delete
    End If
Next
End Sub

Inserting VBA Code

Your code is now ready to run.

  • Press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Play icon in the sub-menu bar to run the macro.
  • From the pop-up Macros window, select the Macro Name, Delete_Row_If_Cell_Contains_Number -> Run.

Executing Code

You will notice that all the rows containing numbers have been deleted from the list.

Delete Row If Cell Contains Numeric Value by Using a Macro in Excel

VBA Code Explanations to Delete a Specific Row from Excel

  • Sub Delete_Row_If_Cell_Contains_Number() -> is for starting program and sub procedure to write the VBA code.
  • Dim Row As Long -> Declaring the variable Row as long data type to store the last row number.
  • Dim Wrk As Long -> Declaring the variable Wrk as long data type to initiate in the For Loop.
    • Long is a numerical data type in VBA The long data type in Excel VBA can hold the values from 0 to 2, 147, 483, 647 for positive numbers, and for the negative number it can hold from 0 to -2, 147, 483, 648.]
  • Row = 15 -> Storing the last row value to the declared Row variable (we stored 15 rows in our worksheet).
  • For Wrk = Row To 1 Step -1 -> To run the For Loop.
  • If IsNumeric(Cells(Wrk, 1)) Then -> Checking each cell whether the value of the cell is a number.
  • Rows(Wrk).Delete -> If the above If Statement is true, then delete the row.
  • End If -> Ending the Statement.
  • Next -> Closing the Loop.
  • End Sub -> Ending the Function.
  • Notice that, in both macros, we were looping through the rows from bottom to up. This is the best approach to check if a cell contains a text value or number value and then delete the rows.

Read More: How to Delete Row Using Macro If Cell Contains 0 in Excel (4 Methods)


Key Point to Consider

The values that we deleted are in Column B in our dataset, hence, we passed 2 as the Column property inside the Cells function. You should pass the Column number according to your dataset. For instance, if the value that you want to delete is in Column A then write If Cells(Wrk, 1), if it is in Column C then write If Cells(Wrk, 3) and so on.


Conclusion

This article showed you how to delete rows if the cells are containing text values or numeric values in Excel by implementing VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions if you have regarding the topic.


Related Articles

Sanjida Ahmed

Sanjida Ahmed

Hello World! This is Sanjida, an Engineer who is passionate about researching real-world problems and inventing solutions that haven’t been discovered yet. Here, I try to deliver the results with explanations of Excel-related problems, where most of my interpretations will be provided to you in the form of Visual Basic for Applications (VBA) programming language. Being a programmer and a constant solution seeker, made me interested in assisting the world with top-notch innovations and evaluations of data analysis.

We will be happy to hear your thoughts

Leave a reply

5 Excel Hacks You Never Knew

Genius tips to help you unlock Excel's hidden features

FREE EMAIL BONUS

ExcelDemy
Logo