• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Conditional delete in a range

Sebasti

New Member
Hi there,

I have this below table which has data from A2 to F6 (which keeps changing every day, can even go upto A2 to F250). Basically i need to delete all the rows except with the word "Dune" in both Column B and Column E. In short i want rows 3 and 4 go from this list. I have this code which seems to be not working. Im a bit bad in VBA too ;-)

Sheets("Data").Select
Range("A1:E1").Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value <> "Dune" Then
Selection.EntireRow.Delete
Else: ActiveCell.Offset(1, 0).Select
End If
Loop

Range("A1").Select

upload_2015-3-16_13-6-58.png
 
There are lot's of way to do this!!

Code:
Sub Delete_Rows()
    Dim i As Long
    Application.ScreenUpdating = False
    With ActiveSheet.Range("A1:F6")
        For i = .Rows.Count To 1 Step -1
            If Not Application.CountIf(Range("A" & i & ":F" & i), "Dune") > 0 Then Rows(i).EntireRow.Delete
        Next
    End With
    Application.ScreenUpdating = True
End Sub
 
Oops..

This would be more dynamic!!

Code:
Sub Delete_Rows2()
    Dim i As Long
    Application.ScreenUpdating = False
    With Sheets("Data")
        For i = .Range("A1").CurrentRegion.Rows.Count To 2 Step -1
            If Not Application.CountIf(Range("A" & i & ":F" & i), "Dune") > 0 Then Rows(i).EntireRow.Delete
        Next
    End With
    Application.ScreenUpdating = True
End Sub
 
Oops..

This would be more dynamic!!

Code:
Sub Delete_Rows2()
Dim i As Long
Application.ScreenUpdating = False
    With Sheets("Data")
        For i = .Range("A1").CurrentRegion.Rows.Count To 2 Step -1
            If Not Application.CountIf(Range("A" & i & ":F" & i), "Dune") > 0 Then Rows(i).EntireRow.Delete
        Next
    End With
Application.ScreenUpdating = True
End Sub

Thank you very much Deepak, you are my savior today :)
 
Back
Top