Hana
Hana

Reputation: 81

vba do until loop doesn't go in although condition is valid

I won't bring the entire code, I'll try to show what is relevant. The following code (which is inside a loop, but it doesn't matter) calls a function (compareStrings) which returns an integer.

Sheet1.Range("S" & i).Value = compareStrings(sheet1.Range("J" & i).Value, sheet1.Range("K" & i).Value)

So basically I have a loop that fills column S with integers. I then sort S column in ascending order. later I have another loop, that is supposed to do something with all the values that are less than 5.

The loop looks like this:

With Sheet1.Range("S" & i)
    Do Until .Value < 5
        If .Value = 0 Then
            'some statement
        Else
            'some statement
        End If
        i = i + 1
    Loop

End With

For some reason it doesn't go in the loop although I have many rows with values that are < 5. I actually tried to change it to <> and it doesn't go in either. It is as if it doesn't see it as an integer, although I have put integers in these cells.

Any ideas?

Thanks

Upvotes: 0

Views: 330

Answers (1)

BigBen
BigBen

Reputation: 49998

"I have another loop that is supposed to do something with all the values that are less than 5."

Your logic doesn't make sense though.

Do Until .Value < 5 will not do anything with values less than 5. It's the same as saying "take action if my value is greater than or equal to 5.

Do While might be a better option.

Upvotes: 1

Related Questions