0

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

10
  • What does the value show up as? I think we will need to see more code. Commented Oct 8, 2018 at 15:42
  • Unfortunately there isn't much for us to help you with without seeing the raw data. I would suggest placing a Debug.Print .Value statement and watching the immediate window. Commented Oct 8, 2018 at 15:42
  • @SJR it shows numbers. If I manually calculate something with these numbers it works (for example =s1 +s2). I don't know what other code might be relevant. I show everything I do with this column.. Commented Oct 8, 2018 at 15:43
  • 1
    Are you sure you want < in Do Until .Value < 5 - i.e. if the value is less than 5, stop and don't do anything? Your logic doesn't seem to make sense, especially if the values are in ascending order. Commented Oct 8, 2018 at 15:44
  • Use F8 to step through the code line by line, checking the values of the variables as you go, and you'll figure out exactly where the issue lies. See Chip Pearson's Debugging VBA. Commented Oct 8, 2018 at 15:44

1 Answer 1

1

"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.

Sign up to request clarification or add additional context in comments.

Comments

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.