Conditional row deletion based on value in a row

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to create a macro that will delete all rows in a worksheet that
have the value 0 in column B. The macro should only delete rows where the
value equals 0, and not rows where the value contains 0 (for example, 10).

Ideally I'd like to have this in a form that I could edit in the future, so
that if down the road I wanted to change the macro to delete all rows where
column F contained the value "Elephant", I could do so without starting from
scratch.
thank you,
Craig
 
Try this (untested)
Sub DeleteThem()
For i=Range("B65536").end(xlup).row to 1 step -1
if cells(i,2).value = 0 then rows(i).Delete
Next
End Sub

Sub DeleteElephantfromF()
For i=Range("F65536").end(xlup).row to 1 step -1
if cells(i,6).value = "Elephant" then rows(i).Delete
Next
End Sub

HTH
Bob Umlas
Excel MVP
 
Craig,

Try code like the following:

Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
For RowNdx = LastRow To 1 Step -1
If Cells(RowNdx, "B").Value = 0 Then
Rows(RowNdx).Delete
End If
Next RowNdx

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
One way is to apply Data|Filter|Autofilter to that column.

Then filtering to show the cells with the value you want to delete.

Then deleting those visible rows.

Record a macro when you do it and you'll have code you can modify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top