Excel - delete rows - if condition


Is there a way to delete rows from a worksheet if the cell in column A is blank? This would be performed on all sheets in the workbook, so I am not sure if there is a macro mechanism for this.

Thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Christian de BellefeuilleProgrammerCommented:
I don't know any function that would perform that in Excel.

I can write a macro for you if you want
tahirihAuthor Commented:
Only if you have the time - this would be wonderful.

Again, I would want to remove rows from all sheets where the cell in column A for that row is blank.

Thank you.
byundtMechanical EngineerCommented:
Here is a macro using the SpecialCells method to get the blank cells.

Sub BlankRowDeleter()
Dim ws As Worksheet
Dim rg As Range
For Each ws In ActiveWorkbook.Worksheets
    Set rg = Nothing
    On Error Resume Next
    Set rg = ws.Columns("A:A").SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If Not rg Is Nothing Then rg.EntireRow.Delete
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Christian de BellefeuilleProgrammerCommented:
well, byundt just shifted me... his solution work just fine
byundtMechanical EngineerCommented:
The manual equivalent to the macro is:
1) Select column A
2) F5 and click the Special Cells... button at the bottom of the resulting dialog
3) Choose the option for Blanks
4) Use the Home...Delete...Delete Sheet Rows menu item (in Excel 2010)
Try this code:
Sub deleterow()

    Application.ScreenUpdating = False
    Dim i As Long
    i = 1
    Do Until i > Cells(65536, "e").End(xlUp).Row
        If Cells(i, "e").Value = "" Then
            i = i + 1
        End If
    Application.ScreenUpdating = True
End Sub

Open in new window

You might have to edit the code depending on the column that you are looking for the blanks in.
tahirihAuthor Commented:
Thank you everyone! I will be revisiting this project in a bit - so your patience is appreciated during the interim.

Patrick MatthewsCommented:

Brad's suggestion should work quite well.  The only caveat is that if you have >16k rows in your source data, then there is a possibility that SpecialCells will fail.  (SpecialCells fails if it returns >8192 distinct areas.)

The usual workaround for that is to sort the data first.

tahirihAuthor Commented:
Thank you everyone.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.