Reputation: 1
Someone please help. I'm trying to write a VBA code that searches for a particular word "K00101" in my excel worksheet "Sales column "c" and then delete the entire row. There are lots of occurrences of the particular word in the worksheet but I just want to delete only one while seaching from bottom to top. My problem is that code is deleting all the rows and I want to stop after 1 delete..
With Sheets("Sales")
Firstrow = .UsedRange.Cells(1).row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "C")
If Not IsError(.Value) Then
If .Value = "T00106DSG5K95" Then .EntireRow.Clear
End If
End With
Next Lrow
End With
Upvotes: 0
Views: 46
Reputation: 23974
If you want to exit out of your loop at any point, you can use Exit For
With Sheets("Sales")
Firstrow = .UsedRange.Cells(1).row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "C")
If Not IsError(.Value) Then
If .Value = "T00106DSG5K95" Then
.EntireRow.Clear
Exit For
End If
End If
End With
Next Lrow
End With
Upvotes: 1