Delete Row in VBA

Ignition1

New Member
Joined
Feb 22, 2011
Messages
49
What is the code I need to delete a row if that row contains a value in Column W which is other than "Live"?

So I tried -

If Columns("W").Value = "Dormant" Then EntireRow.Delete

Which is clearly wrong. I had a look on the net but it didn't help. Bit stumped on this one.

Edit - I should point out that "Dormant" is one of about 5 values which isn't "Live"
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try

Code:
Sub ColW()
Dim LR As Long, i As Long
LR = Range("W" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("W" & i).Value <> "Live" Then Rows(i).Delete
Next i
End Sub
 
Upvote 0
Thanks VoG - that code worked perfectly, though I don't understand it entirely.

rsxchin's looks quite straight forward but not sure how I would go about referring to a range of rows.

I'm assuming it would go -

if cells(2:2000,"W").Value <> "Live" Then EntireRow.Delete

Though it returned an error.
 
Upvote 0
Mine just loops backwards through the range. This should be faster

Code:
Sub Del_W()
With Columns("W")
    .AutoFilter field:=1, Criteria1:="<>Live"
    .Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
That one I understand (sort of). Thanks again.

I'm about half way through my VBA for Dummies book...Offset, With...that should all be clear to me by the end!
 
Upvote 0
I've been modifying that code to work at filtering out other rows but I've come across an issue with deleting any rows above a certain date.



Sub deletedate()
Dim LR As Long, i As Long
Range("Z2:Z3000").Select
Selection.NumberFormat = "dd/mm/yyy;@"
LR = Range("Z" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
If Range("Z" & i).Value > 1 / 12 / 2012 Then Rows(i).delete
Next i
End Sub

It deletes all the rows!
 
Upvote 0
It deletes all the rows!

Obviously, since 1 divided by 12 divided by 2012 is a very small number.

Use for instance:

Code:
DateSerial(2012, 12, 1)

to compare to. There are other possibilities too, like having the date as a string.

Wigi
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,786
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top