Solved

Excel - delete rows - if condition

Posted on 2011-02-23
10
336 Views
Last Modified: 2012-05-11
Hi,

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

Expert Comment

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

I can write a macro for you if you want
0
 

Author Comment

by:tahirih
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.
0
 
LVL 81

Accepted Solution

by:
byundt earned 350 total points
ID: 34963666
Here is a macro using the SpecialCells method to get the blank cells.

Brad
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
Next
End Sub

Open in new window

0
 
LVL 10

Expert Comment

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

Expert Comment

by:byundt
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)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Assisted Solution

by:gtgloner
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
            Rows(i).delete
        Else
            i = i + 1
        End If
    Loop
 
    Application.ScreenUpdating = True
 
End Sub

Open in new window

0
 
LVL 17

Expert Comment

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

Author Comment

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

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34964744
tahirih,

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.

Patrick
0
 

Author Closing Comment

by:tahirih
ID: 34965737
Thank you everyone.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ifna and iferror 1 32
Automating Excel Weekly Report 13 53
Sum iF  based on a null cell 11 29
Excel 2010 - Comparison of texts in a cell 14 15
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now