Deleting Rows

  • Thread starter Thread starter simoncohen
  • Start date Start date
S

simoncohen

Please help with some code for the following .....

I need to create a loop to scan down a column and if the contents of
cell are say "D" then delete this row and go onto the next entry.

Thank
 
Hi
try something like the following:
Sub delete_rows()
Dim RowNdx As Long
Dim LastRow As Long
Application.ScreenUpdating = False
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).row
For RowNdx = LastRow To 1 Step -1
with Cells(RowNdx, "A")
if .value = "value" then
Rows(RowNdx).Delete
End If
end with
Next RowNdx
Application.ScreenUpdating = True
End Sub
 
Simon,

the following piece of code can be amended easily to
handle this sort of thing. This version deletes empty rows
in a range. To amend for your example (and assuming your
values are in column A), change the If...Then line to

If Cells(r,1).value = "D" Then

Cheers, Pete

Sub DeleteEmptyRows()

Dim LastRow As Integer
Dim r As Integer
Application.ScreenUpdating = False

LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count

For r = LastRow To 1 Step -1
If Application.WorksheetFunction.CountA(Rows(r)) = 0 Then
Rows(r).Delete
End If
Next r

Application.ScreenUpdating = True
End Sub
 

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