Excel - delete rows - if condition

Posted on 2011-02-23
Last Modified: 2012-05-11

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
Question by:tahirih
  • 3
  • 2
  • 2
  • +2
LVL 10

Expert Comment

ID: 34963591
I don't know any function that would perform that in Excel.

I can write a macro for you if you want

Author Comment

ID: 34963645
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.
LVL 81

Accepted Solution

byundt earned 350 total points
ID: 34963666
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 10

Expert Comment

ID: 34963678
well, byundt just shifted me... his solution work just fine
LVL 81

Expert Comment

ID: 34963691
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)
LVL 17

Assisted Solution

gtgloner earned 150 total points
ID: 34963770
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

LVL 17

Expert Comment

ID: 34963789
You might have to edit the code depending on the column that you are looking for the blanks in.

Author Comment

ID: 34963868
Thank you everyone! I will be revisiting this project in a bit - so your patience is appreciated during the interim.

LVL 92

Expert Comment

by:Patrick Matthews
ID: 34964744

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.


Author Closing Comment

ID: 34965737
Thank you everyone.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question