Excel - delete rows in a given range

Asked By Wen on 27-Nov-09 10:27 AM
Anyone has a good sub to delete from a given range any row with blank index
cell?
For example, I have a table range with column A titled "Name" as index
column. I need a sub that deletes any row where column A is blank, but say,
column C has non-blank value. it is not the same as deleting all blank rows
in a range.
TIA.
Wen


Don Guillett replied to Wen on 27-Nov-09 11:07 AM
Option Explicit
Sub deletesortedduplicates()
Dim i As Long
Dim MC As Long
MC = 1 'col A
For i = Cells(Rows.Count, MC).End(xlUp).Row To 1 Step -1
If Cells(i, MC) = "" and cells(i,mc+2)<>""  Then Rows(i).Delete
Next i
End Sub


--
Don Guillett
Microsoft MVP Excel
SalesAid Software
dguillett1@austin.rr.com
Ryan H replied to Wen on 27-Nov-09 11:20 AM
Just adjust the worksheet name, first row, and last row variables and it
should work for you.  If you do not want the entire row deleted let me know.
If this helps click "YES" below.

Sub DeleteRows()

Dim wks As Worksheet
Dim lngFirstRow As Long
Dim lngLastRow As Long

Set wks = Sheets("Sheet1")
lngFirstRow = 2
lngLastRow = wks.Cells(Rows.Count, "A").End(xlUp).End

' clears empty rows from sheet
With wks
For i = lngLastRow To lngFirstRow Step -1
If Trim(.Cells(i, "A")) = "" Then
.Cells(i, "A").EntireRow.Delete Shift:=xlUp
End If
Next i
End With
End Sub
--
Cheers,
Ryan
Rick Rothstein replied to Wen on 27-Nov-09 12:06 PM
I am pretty sure this non-looping macro will do what you want (assuming your
values in Column C are constants and not formula results)...

Sub DeleteRows()
On Error Resume Next
Intersect(Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow, _
Columns("C").SpecialCells(xlCellTypeConstants).EntireRow).Delete
End Sub

If Column C contains formulas, not constants, then replace the
xlCellTypeConstants argument with xlCellTypeFormulas in the above code.

--
Rick (MVP - Excel)
fisch4bill replied to Ryan H on 30-Nov-09 11:08 AM
In order for this code to work for me, I had to change the line:

lngLastRow = wks.Cells(Rows.Count, "A").End(xlUp).End

to:

lngLastRow = wks.Cells(Rows.Count, "A").End(xlUp).Row

Using the original line resulted in Run-time error '450': "Wrong number of
arguments or invalid property assignment"

Otherwise, once I changed the reference to the value I was testing for it
worked flawlessly and the logic was just what I needed.

Thanks Ryan.