Delete rows with blank cells in a certain column using VBA in Microsoft Excel

In this article, we will create a macro to delete incomplete records which contain blank cells.

Raw data consists of some sample data, which includes Name, Age and Gender. It also contains some blank cells.

ArrowRawData

We want to remove those records which contain blank cells.

ArrowOutput

Logic explanation

We have created “BlankRowDeletion” macro to delete incomplete records. It searches and selects blank cells and then deletes the entire row which contains a blank cell.

Code explanation

Set Rng = Range("A9:C" & LastRow)

The above code is used to create a range object for the given data.

Rng.SpecialCells(xlCellTypeBlanks).Select

The above code is used to select blank cells within the specified range.

Selection.EntireRow.Delete

The above code is used to delete the entire row for the selected cell.

 

Please follow below for the code


Option Explicit

Sub BlankRowDeletion()

'Declaring variables
Dim LastRow As Long
Dim Rng As Range

'Getting row number of last cell
LastRow = Range("A1").SpecialCells(xlCellTypeLastCell).Row

'Selecting all data
Set Rng = Range("A9:C" & LastRow)

'Selecting Blank cells
Rng.SpecialCells(xlCellTypeBlanks).Select

'Deleting complete row
Selection.EntireRow.Delete

Range("A9").Select

End Sub

 

If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at info@exceltip.com

Comments

  1. I tried to use the code in this article and it deleted every row in the spreadsheet. I am trying to delete all the rows in the spreadsheet that have no values in the cells in column G so I modified the code to use that range. I think it is very close though. Thank you!!

  2. Thank you for this. I don't use Excel macros often and this was just what I needed. Easy to adapt for my purposes and it worked on the first try.

  3. "Consider the Following code to batch-delete the unused names in a workbook.

    Dim i As Integer

    For i = 1 To ActiveWorkbook.Names.Count
    ActiveWorkbook.Names(i).Delete
    Next i "

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.