mathsicist
mathsicist

Reputation: 191

The vba code is executing although the condition is not true

enter image description here The code reads a list of strings from column from "HistoryUrgents" sheet and then it moves into another sheet named "Sheet1" and there it finds them and paints rows containing the strings. It works until it read a second string (it paints correct all rows containing first string). The code just stuck in a continuous loop. If we would force close it by ctrl + break then we got a popup "Run-time error '91'"

As you see on attached screenshot the "c" variable is nothing and although it the If Not c Is Nothing Then passed it.

Upvotes: 0

Views: 165

Answers (2)

Darren Bartrup-Cook
Darren Bartrup-Cook

Reputation: 19857

In your Do loop your range and cells aren't necessarily referencing Sheet1 - they're referencing the currently active sheet.

Add a dot (period) before all range & cell references in your With...End With block.

Set c = .Cells.Find(What:=

and

.Range(.Cells(c.ROW, START_MARK), .Cells(

I haven't been able to test this as you've posted a picture of your code - if you'd posted your code and wrapped it in code tags I could've copied it to a workbook.

Upvotes: 2

andshrew
andshrew

Reputation: 418

The reason that you're getting run-time error 91 is because you're trying to access the property of an object that doesn't exist. If c is Nothing then c.Address doesn't exist. I don't know why this isn't throwing an error automatically, are you doing something to override the error handler or to suppress pop-up messages?

My suggestion would be to move your c.Address <> firstAddress check into the body of the loop.

Eg.

Do
    If c.Address <> firstAddress Then
        c.Select
        ' ... insert rest of your code ...
        ' ...
    End If
Loop While Not c Is Nothing

Upvotes: 0

Related Questions