VBA Delete Row

Last Updated :

21 Aug, 2024

Blog Author :

Edited by :

Ashish Kumar Srivastav

Reviewed by :

Dheeraj Vaidya, CFA, FRM

Table Of Contents

arrow

Excel VBA Delete Row

Normally in an Excel worksheet, we have two different methods to delete rows: the keyboard shortcut and the right-click and insert method. But in VBA, we must use the "Delete" command and worksheet statement to delete any rows. The trick is that if we need to delete a single row, we give a single row reference, but for multiple columns, we give multiple row references.

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

This article 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

How to Delete Row?

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 from 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, we have entered a few characters in an Excel sheet.

vba delete example 1.4

If we 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 deleted the row using the CELLS property. Now, we will see how to delete by using the ROWS property.

vba delete example 1.5

Now, we need to mention what is the row we need to delete. For example, we need to delete the 5th row.

vba delete example 1.6

Now, use the "EntireRow" property.

vba delete example 1.7

After selecting the property, what do we need to do? First, 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. For example, assume you have some values from A1 to A6 cells.

vba delete example 2.1

Now, we want to delete the first five rows, so we 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, we want to use the word "EntireRow" property.

Code:

Sub DeleteRow_Example4()

Range("A1:A5").EntireRow

End Sub
vba delete example 2.3

In this 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 the cell value in VBA. For example, we have "Yes" and "No" values from cells A1 to A10.

example 3.1

We need to delete the rows with the value "No." We must use the function "IF" with loops to delete all the rows with the value of "No" to perform this task.

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, we have the below set of data.

example 4.1

All the colored cells are blank, so we must 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

It will identify the blank cells in the range A1 to F10. If it finds any blank cells, it will delete the entire row.

example 4.2 gif

The problem with this code is it will only delete the blank cell's row in the range A1 to F10. But if any cells are blank in any other cells, it will not delete them. So, keeping this in mind, we 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 rows in the selected range.

Recommended Articles

This article has been a guide to VBA Delete Row. Here, we discussed how to delete rows using VBA codes and practical examples. Below are some useful Excel articles related to VBA: -