1

I have this code, which I want to loop through all sheets and delete entire rows based on the value "completed" located anywhere in the sheet...This runs but only on the active sheet, I cant figure a good way to have it loop through every sheet till done...any help is greatly appreciated! ty

Private Sub TestDeleteRows()
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim sFirstAddress As String

strSearch = "Completed"
Set rDelete = Nothing

Application.ScreenUpdating = False

With Sheet1.Columns("A:AO")
Set rFind = .Find(strSearch, 
LookIn:=xlValues, LookAt:=xlPart,   SearchDirection:=xlNext, MatchCase:=False)
If Not rFind Is Nothing Then
    sFirstAddress = rFind.Address
    Do
        If rDelete Is Nothing Then
            Set rDelete = rFind
        Else
            Set rDelete = Application.Union(rDelete, rFind)
        End If
        Set rFind = .FindNext(rFind)
    Loop While Not rFind Is Nothing And rFind.Address <> sFirstAddress

    rDelete.EntireRow.Delete

End If
End With
Application.ScreenUpdating = False
End Sub
1
  • Can this be altered to do the following: Delete a row based on a column name and cell value? or multiple column names and cells? the example being: i want to delete anything that has a value of 'Complete' in column named 'Status' but I may also want to delete anything 'Complete' in Column named 'Second Status' as well..ty Commented Mar 14, 2017 at 20:14

1 Answer 1

2
Private Sub TestDeleteRows()
Dim rFind As Range
Dim rDelete As Range
Dim strSearch As String
Dim sFirstAddress As String
Dim sh As Worksheet

strSearch = "Completed"
Set rDelete = Nothing

Application.ScreenUpdating = False
For Each sh In ThisWorkbook.Sheets
With sh.Columns("A:AO")
Set rFind = .Find(strSearch, LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlNext, MatchCase:=False)
If Not rFind Is Nothing Then
    sFirstAddress = rFind.Address
    Do
        If rDelete Is Nothing Then
            Set rDelete = rFind
        Else
            Set rDelete = Application.Union(rDelete, rFind)
        End If
        Set rFind = .FindNext(rFind)
    Loop While Not rFind Is Nothing And rFind.Address <> sFirstAddress

    rDelete.EntireRow.Delete
    Set rDelete = Nothing
End If
End With
Next sh
Application.ScreenUpdating = False
End Sub
Sign up to request clarification or add additional context in comments.

13 Comments

Hello, This has given the error "Compile Error:" For Each may only iterate over a collection object or an array" What happened?
Sorry, can you try the edited code? I was using the count method but removed that so that its only accessing the sheet collection itself. Sorry about that.
Sorry to be Trouble now it says "Runtime Error 1004: Method "Union of object _Application Failed" for the line under else..but still seems to work on sheet 1
okay, perhaps we need to reset rDelete to nothing since the if statement doesn't evaluate to True if you're using multiple sheets and it runs at least once. Try the edited version and let me know if that works for you.
Hello, I think you were right! it now works with no error! ty so much ^^
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.