VBA Delete Row

Excel VBA Delete Row

Normally in excel worksheet, we have two different methods to delete rows one being the keyboard shortcut and another by using the right-click and insert method but in VBA we have to use delete command and worksheet statement to delete any rows together, the trick to this is that if we need to delete a single row we give a single row reference but for the multiple columns we give multiple row references.

Using VBA Delete Row Method,  we can delete all the blank rows, and we can delete the row based on cell value. We can also delete the entire row if any of the cells are blank.

In this article, we will discuss the method “VBA Delete Row.” Keep yourself occupied for the next 15 to 20 minutes to learn about the concept.

VBA Delete Row

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Delete Row (wallstreetmojo.com)

How to Delete Row?

You can download this VBA Delete Row Excel Template here – VBA Delete Row Excel Template

Example #1

In VBA, we need to mention the row we are deleting.

Code:

Sub DeleteRow_Example1()
Cells(1, 1)
End Sub
vba delete example 1.1

Cells (1, 1) means first-row first column i.e., A1 cell. Then we use the method “delete.”

Code:

Sub DeleteRow_Example1()
Cells(1, 1).Delete
End Sub
vba delete example 1.2

Now this will delete the first cell. All the right side values will shift one cell to the left.

vba delete example 1.2

Example #2

If you want to delete the entire row, we need to use the property “EntireRow,” then we need to use the method “delete” to delete the entire row of the cell we have selected.

Code:

Sub DeleteRow_Example2()
Cells(1, 1).EntireRow.Delete
End Sub
vba delete example 1.3

For example, I have entered a few characters in an excel sheet as follows.

vba delete example 1.4

Now, if I run this code, it will delete the entire row, not a single cell.

vba delete example 1.4

Example #3

We can delete the row by using several ways. In the above example, we have deleted the row by using CELLS property. Now we will see how to delete by using ROWS property.

vba delete example 1.5

Now we need to mention what is the row we need to delete. Let’s say we need to delete 5th row.

vba delete example 1.6

Now use the property “EntireRow” property.

vba delete example 1.7

After selecting the property, what we need to do i.e., method. We need to delete the row.

Code:

Sub DeleteRow_Example3()
Rows(5).EntireRow.Delete
End Sub
vba delete example 1.8

So, this code will delete the 5th row.

Example #4

Delete Multiple Rows by Using Range Object

How do we delete multiple rows?

We can use the VBA RANGE object to delete more than one row. Assume you have some values from A1 to A6 cells.

vba delete example 2.1

Now I want to delete the first 5 rows, so I can reference these rows by using the Range object as “Range (“A1: A5”)”

Code:

Sub DeleteRow_Example4()
Range ("A1: A5")
End Sub
vba delete example 2.2

Now I want to use the word “EntireRow” property.

Code:

Sub DeleteRow_Example4()

Range("A1:A5").EntireRow

End Sub
vba delete example 2.3

In this entire row, we need to perform the method of deleting, so use the Delete method.

Code:

Sub DeleteRow_Example4()

Range("A1:A5").EntireRow.Delete

End Sub
vba delete example 2.4

Now this will delete the selected rows.

vba delete example 2.4

Example #5

Delete Rows Based On Cell Value

We can also use this “EntireRow.Delete” method to delete the row based on cell value in VBA. For example, I have Yes & No values from cell A1 to A10.

example 3.1

Now we need to delete the rows which have the value “No.” To perform this task, we need to use the function “IF” with loops to delete all the rows which have the value of “No.”

The below code will do the job for us.

Code:

Sub DeleteRow_Example5()

Dim k As Integer 

For k = 10 To 1 Step -1
If Cells(k, 1).Value = "No" Then
Cells(k, 1).EntireRow.Delete
End If
Next k

End Sub
example 3.2

Example #6

Delete All the Blank Cells Rows

There are situations where we need to delete the entire row if any of the cells in the range are blank. For example, I have below set of data.

example 4.1

All the colored cells are blank, so I need to delete the entire row. We can perform this task with two sets of code. Below is the code.

Code:

Sub DeleteRow_Example6()

Range("A1:F10").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
example 4.2

This will identify the blank cells in the range A1 to F10.IF any blank cells are found, they will delete the entire row.

example 4.2 gif

The problem with this code is it will only delete the blank cell’s row only in the range A1 to F10, but if any cells are blank in any other cells, it will not delete. So keeping this in mind, I have written one more code.

Code:

Sub DeleteRow_Example7()

Dim RangeToDelete As Range
Dim DeletionRange As Range

Set RangeToDelete = Application.InputBox("Please select the range", "Blank Cells Rows Deletion", Type:=8)

Set DeletionRange = RangeToDelete

RangeToDelete.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
example 4.3

When you run this code, firstly, it will ask you to select the range with an input box appearing in front of you.

example 4.4

After selecting the range, you need to click on OK. It will delete all the blank cells rowsDelete All The Blank Cells RowsThere are several methods for deleting blank rows from Excel: 1) Manually deleting blank rows if there are few blank rows  2)  Use the formula delete 3) Use the filter to find and delete blank rows.read more in the selected range.

Recommended Articles

This has been a guide to VBA Delete Row. Here we discussed how to delete rows using VBA codes along with practical examples. Below are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>