VBA to Delete Entire Row based on Cell Value in Excel (3 Methods)

Sometimes we need to remove an entire specific row from a huge dataset of rows 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. In this article, we will show you how to delete an entire row based on a specific cell value in Excel utilizing the VBA macro.


Download Practice Template

You can download the free practice Excel template from here.


3 Methods on Implementing VBA to Delete an Entire Row Based on Cell Value in Excel

In this section, we will show you how to delete an entire row containing specific values from the Excel worksheet using VBA. 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 a method that will delete the entire row from the dataset.

And now we will show you how to delete an entire row if the cell contains a specific text value, numeric value or empty value in Excel by implementing the VBA macro.


1. Embed VBA to Delete Entire Row If Cell Contains a Specific Text Value in Excel

Consider the following dataset from where we want to delete the whole record of the name “Ron”.

dataset of excel vba delete entire row based on cell text value

The steps to delete the whole row of “Ron” 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.

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

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

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.

You will notice that the entire row of “Ron” has been deleted from the dataset.

result of excel vba delete entire row based on cell text value

VBA Code Explanation:

  • Sub Delete_Row_Cell_Contains_Text() -> is for starting the 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 i As Long -> Declaring the variable i 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.
  • For i = Row To 1 Step -1 -> To run the For Loop.
  • If Cells(i, 1) = “Ron” Then -> Checking each cell whether the value of the cell is equal to “Ron” (you can put any string here according to your need).
  • Rows(i).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: How to Delete Rows in Excel: 7 Methods


2. Insert VBA to Delete Entire Row If Cell Contains a Specific Numeric Value in Excel

Suppose, from a dataset like below, you want to delete the whole record of a specific Name based on it’s ID number. With the VBA code you can achieve that too.

dataset of excel vba delete entire row based on cell numeric value

The steps to delete the whole row of “Ron” based on his ID number by VBA code are given below.

Steps:

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • In the code window, copy the following code and paste it.
Sub Delete_Row_Cell_Contains_Number()
Dim Row As Long
Dim i As Long
Row = 10
For i = Row To 1 Step -1
If IsNumeric(Cells(i, 1)) = 103 Then
Rows(i).Delete
End If
Next
End Sub

Your code is now ready to run.

  • From the pop-up Macros window, select the Macro Name, Delete_Row_Cell_Contains_Number -> Run.

You will notice that the entire row of Ron’s ID number 103 has been deleted from the dataset.

result of excel vba delete entire row based on cell numeric value

VBA Code Explanation:

  • Sub Delete_Row_Cell_Contains_Number() -> is for starting the 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 i As Long -> Declaring the variable i 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 = 10 -> Storing the last row value to the declared Row variable.
  • For i = Row To 1 Step -1 -> To run the For Loop.
  • If IsNumeric(Cells(i, 1)) = 103 Then -> Checking each cell whether the value of the cell is a specific number.
  • Rows(i).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.

Key Point to Remember

In some cases, Excel’s IsNumeric property doesn’t always produce the correct result in VBA. In that case, instead of writing the code line If IsNumeric(Cells(i, 1)) = 103 Then, write If Cells(i, 1) = 103 Then in your code module.

Read More: VBA Macro to Delete Row if Cell Contains Value in Excel (2 Methods)


Similar Readings:


3. Implement VBA to Delete Entire Row If Cell Contains Empty Value in Excel

Consider the following dataset, where there is a row containing an empty cell.

dataset of excel vba delete entire row based on cell empty value

Now, we will show you how to delete an entire row if it contains an empty cell.

The steps to delete the whole row containing an empty cell by VBA code are given below.

Steps:

  • Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
  • In the code window, copy the following code and paste it.
Sub Delete_Row_Blank_Cell()
Dim Row As Long
Dim i As Long
Row = 15
For i = Row To 1 Step -1
If Cells(i, 1) = "" Then
Rows(i).Delete
End If
Next
End Sub

Your code is now ready to run.

  • In the same way as before, Run the code and you will notice that the entire row containing the empty cell has been deleted from the dataset.

result of excel vba delete entire row based on cell empty value

VBA Code Explanation:

  • Sub Delete_Row_Blank_Cell() -> is for starting the 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 i As Long -> Declaring the variable i 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.
  • For i = Row To 1 Step -1 -> To run the For Loop.
  • If Cells(i, 1) = “” Then -> Checking each cell whether the value contains null string.
  • Rows(i).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 the 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 Selected Rows with Excel VBA (A Step-by-Step Guideline)


Key Point to Consider

The values that we deleted are in Column A in our dataset, hence, we passed 1 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 B then write If Cells(i, 2), if it is in Column C then write If Cells(i, 3) and so on.


Conclusion

This article showed you how to delete an entire row if the cell contains text value, numeric value or empty value in Excel by implementing the VBA macro. I hope this article has been very beneficial to you. Feel free to ask any questions 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.

2 Comments
  1. I am unable to run the macro which identify the row with value 103. It doesn’t seem to work. I’ve downloaded your excel file, and also can’t seem to obtain the result. Please assist. Thank you.

    • Hi James, In some cases, Excel’s IsNumeric property doesn’t always produce the correct result in VBA. Could you please try If Cells(i, 1) = 103 Then instead of If IsNumeric(Cells(i, 1)) = 103 Then in your code?

Leave a reply

ExcelDemy
Logo