How to Delete Entire Row Based on Cell Value Using VBA in Excel

Get FREE Advanced Excel Exercises with Solutions!

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 code.
In the following GIF, you can see the overview of using VBA to delete entire rows based on cell value. Now, let’s go through the following article so that you can easily do the task.

Overview GIF to Delete Entire Row Based on Cell Value in Excel VBA


Download Practice Workbook

You can download the free practice Excel template from here.


3 Examples of Using VBA Code to Delete Entire Row Based on Cell Value in Excel

You can see the Name, Department, and Grade columns in the following dataset. After that, using this dataset, we will go through 3 easy examples to use VBA to delete the entire row based on cell value in Excel. Here, we used Excel 365. You can use any available Excel version.

Dataset for VBA to Delete Entire Row Based on Cell Value in Excel


1. Applying VBA Code to Delete Entire Row Based on Text String in Cell

In this method, we will use Excel VBA to delete the entire row based on specific text in a cell.
To do so, we will consider the following dataset from where we want to delete the whole record of the name Ron.

Dataset to use Excel VBA to delete entire row based on specific text in a cell

Steps:

  • In the first place, go to the Developer tab >> select Visual Basic.
  • This will open up the VBA Editor window.
  • Here, you can press Alt + F11 on your keyboard to open the VBA Editor window.

Using Developer Tab

  • Therefore, you can see the VBA Editor window.
  • Then, from the Insert tab >> select Module.

Inserting Module

  • Moreover, we will type the following code in the Module.
Sub Delete_Row_Cell_Contains_Text()
Dim s_row As Long
Dim c_i As Long
s_row = 15
For c_i = s_row To 1 Step -1
   If Cells(c_i, 2) = "Ron" Then
      Rows(c_i).Delete
   End If
Next
End Sub

Inserting Code in the Module

Code Breakdown

  • Sub Delete_Row_Cell_Contains_Text() -> is for starting the program and sub procedure to write the VBA code.
  • Dim s_row As Long -> Declaring the variable s_row as long data type to store the last row number.
  • Dim c_i As Long -> Declaring the variable c_i as long data type to initiate 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.
  • s_row = 15 -> Storing the last row value to the declared s_row.
  • For c_i = Row To 1 Step -1 -> To run the FOR loop.
  • If Cells(c_i, 2) = “Ron” Then -> Check each cell whether the value of the cell is equal to “Ron” (you can put any string here according to your need).
  • Rows(c_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 Code.
  • After that, Save the code >> return to the Worksheet.
  • Then, from the Developer tab >> select Macros.

Employing Macros

  • At this point, a Macro window will appear.
  • Then, we will select Delete_Row_Cell_Contains_Text as the Sub.
  • Click on Run.

Running the Code

  • Therefore, you will notice that the entire row of Ron has been deleted from the dataset.

Result After Using VBA

  • For a better understanding, see the following GIF.

GIF for Using VBA to delete an entire row based on a cell’s text in Excel

Read More: How to Delete Rows in Excel: 7 Methods


2. Deleting Entire Row by Using Excel VBA If Cell Contains a Specific Numeric Value

We modified the previous dataset by adding an ID column. Suppose, from a dataset like below, you want to delete the whole record of a specific Name based on its ID number. Therefore, we will use VBA to delete the entire row based on a numeric value in a cell in Excel.

Dataset to delete entire row based on a numeric value in a cell using Excel VBA

Steps:

  • In the beginning, we will follow the steps of Method-1 to bring out the Module.
  • Then, we will type the following code in the Module.
Sub Delete_Row_Cell_Contains_Number()
Dim s_row As Long
Dim c_i As Long
s_row = 10
For c_i = s_row To 1 Step -1
    If Cells(c_i, 2) = 103 Then
      Rows(c_i).Delete
    End If
Next
End Sub

Employing Code in Module

Code Breakdown

  • Sub Delete_Row_Cell_Contains_Number() -> is for starting the program and sub procedure to write the VBA code.
  • Dim s_row As Long -> Declaring the variable Row as long data type to store the last row number.
  • Dim c_i As Long -> Declaring the variable c_i as a 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.]
  • S_row = 10 -> Storing the last row value to the declared  s_row variable.
  • For c_i = Row To 1 Step -1 -> To run the For loop.
  • If IsNumeric(Cells(c_i, 2)) = 103 Then -> Checks each cell whether the value of the cell is a specific number.
  • Rows(c_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 Code.
  • After that, Save the code >> return to the Worksheet.
  • Then, from the Developer tab >> select Macros.

Use of Macros feature

  • At this point, a Macro window will appear.
  • Then, we will select Delete_Row_Cell_Contains_Number as the Sub.
  • Click on Run.

Use of Run Option

  • As a result, you will notice that the entire row of Ron’s ID number 103 has been deleted from the dataset.

Result after using VBA

Note: 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(c_i, 2)) = 103 Then, write If Cells(c_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. Implementing VBA Code to Remove Entire Row If Cell Is Empty

In this method, we will implement VBA to delete the entire row based on the blank cell value in Excel. For this purpose, we will consider the following dataset, where there is a row containing an empty cell.

Dataset to implement VBA to delete entire row based on blank cell value in Excel

Steps:

  • First of all, we will follow the steps of Method-1 to bring out the Module.
  • Then, we will type the following code in the Module.
Sub Delete_Row_Blank_Cell()
Dim s_row As Long
Dim c_i As Long
s_row = 20
For c_i = s_row To 4 Step -1
    If Cells(c_i, 2) = "" Then
       Rows(c_i).Delete
    End If
Next
End Sub

Inserting Code in Module

Code Breakdown

  • Sub Delete_Row_Blank_Cell() -> is for starting the program and sub procedure to write the VBA code.
  • Dim s_row As Long -> Declaring the variable s_row as long data type to store the last row number.
  • Dim c_i As Long -> Declaring the variable c_i as long data type to initiate 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.
  • For c_i = s_row To 4 Step -1 -> To run the For loop.
  • If Cells(c_i, 2) = “” Then -> Checking each cell whether the value contains a 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 Code.
  • After that, Save the code >> return to the Worksheet.
  • Then, from the Developer tab >> select Macros.

Using Developer Tab to bring Macros

  • At this point, a Macro window will appear.
  • Then, we will select Delete_Row_Blank_Cell as the Sub.
  • Click on Run.

Running the code

  • Therefore, you will notice that the row containing the blank cell has been deleted.

The Result after inserting VBA to delete a row of blank cell

Note: 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 Points 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 property. You should give the Column number according to your dataset. For instance, if the value that you want to delete is in Column C then write If Cells(i, 3), if it is in Column D then write If Cells(i, 4), and so on.

Practice Section

You can download the above Excel file and practice the explained methods.

Practice Section


Conclusion

In this article, we describe 3 easy examples to use VBA to delete an entire row based on cell value in Excel. We hope this article has been very beneficial to you. Feel free to ask any questions regarding the topic. You can visit our website Exceldemy for more related articles.


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

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo