[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Excel - delete rows - if condition

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
tahirih
Asked:
tahirih
  • 3
  • 2
  • 2
  • +2
2 Solutions
 
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
0
 
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.
0
 
byundtCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Christian de BellefeuilleProgrammerCommented:
well, byundt just shifted me... his solution work just fine
0
 
byundtCommented:
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
 
gtglonerCommented:
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
 
gtglonerCommented:
You might have to edit the code depending on the column that you are looking for the blanks in.
0
 
tahirihAuthor Commented:
Thank you everyone! I will be revisiting this project in a bit - so your patience is appreciated during the interim.

0
 
Patrick MatthewsCommented:
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
 
tahirihAuthor Commented:
Thank you everyone.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now