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.
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.
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.
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.
- Therefore, you can see the VBA Editor window.
- Then, from the Insert tab >> select 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
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.
- At this point, a Macro window will appear.
- Then, we will select Delete_Row_Cell_Contains_Text as the Sub.
- Click on Run.
- Therefore, you will notice that the entire row of Ron has been deleted from the dataset.
- For a better understanding, see the following GIF.
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.
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
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.
- At this point, a Macro window will appear.
- Then, we will select Delete_Row_Cell_Contains_Number as the Sub.
- Click on Run.
- As a result, you will notice that the entire row of Ron’s ID number 103 has been deleted from the dataset.
Read More: VBA Macro to Delete Row if Cell Contains Value in Excel (2 Methods)
Similar Readings:
- How to Remove Empty Rows in Excel (11 Methods)
- Macro to Delete Row in Excel If Cell is Blank
- How to Delete Filtered Rows in Excel (5 Methods)
- Delete Hidden Rows in Excel (3 Methods)
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.
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
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.
- At this point, a Macro window will appear.
- Then, we will select Delete_Row_Blank_Cell as the Sub.
- Click on Run.
- Therefore, you will notice that the row containing the blank cell has been deleted.
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.
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.
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?