400 Error Excel Macro

Viewed 21388

I'm trying to run a macro that will delete rows that don't contain a particular value in column B. Here's my code:

Sub deleteRows()
    Dim count As Integer
    count = Application.WorksheetFunction.CountA(Range("AF:AF"))
    Dim i As Integer
    i = 21
    Do While i <= count
        If (Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search("OSR Platform", Range("B" & i))) = False) Then
            If (Application.WorksheetFunction.IsNumber(Application.WorksheetFunction.Search("IAM", Range("B" & i))) = False) Then
                Rows(i).EntireRow.Delete
                i = i - 1
                count = count - 1
            End If
        End If
        i = i + 1
    Loop
End Sub

Now what it SHOULD be doing is the following:

1.) Find the number of rows to go through and set that as count (this works)

2.) Start at row 21 and look for "OSR Platform" and "IAM" in column B [this kind of works (see below)]

3.) If it finds neither, delete the entire row and adjust the count and row number as necessary (this works)

For some reason, whenever the code gets to the first If statement, an error window with a red X pops up that just says "400." As far as I can tell, I have written everything syntactically soundly, but clearly there's something wrong.

2 Answers
Related