2

Trying to return a L, M, H value based on the values of two inputs. Here is what I have:

If (80 <= x <= 120 And y > 120) Or (x > 120 And y > 120) Or (x > 120 And 80 <= y <= 120) Then
    CDI = "H"

ElseIf (x < 80 And y <= 120) Or (x < 120 And y < 80) Then
    CDI = "L"

ElseIf (x < 80 And y > 120) Or (80 <= x <= 120 And 80 <= y <= 120) Or (x > 120 And y < 80) Then
    CDI = "M"

End If

The values being returned aren't what I need. For example, (0,291) returns H when it should clearly be M. Why?

2 Answers 2

3

The conditions with the variable in the middle (even if they are acceptable to the VBA interpreter) are almost certainly not going to produce the result you expect. Change all such conditions to the equivalent of the form:

variable condition constant

Example: Change

80 <= x <= 120

to

x >= 80 and x <= 120
Sign up to request clarification or add additional context in comments.

4 Comments

DUH, thanks so much that fixed it. I'm new to VBA but made the poor assumption it could act as python does. Thanks again!
@Robert, so it handles the variable condition variable as an or statement?
@Elias: I think it probably has more to do with order of operations, and the way results of calculations are intepreted as boolean values, but I haven't really checked.
I think you're right. False < 120 = True, because False = 0 And True = 1
1

Try this:

If (x >= 80 And x <= 120 And y > 120) Or (x > 120 And y > 120) Or (x > 120 And y >= 80 And y <= 120) Then
    CDI = "H"

ElseIf (x < 80 And y <= 120) Or (x < 120 And y < 80) Then
    CDI = "L"

ElseIf (x < 80 And y > 120) Or (x >= 80 And x <= 120 And y >= 80 And y <= 120) Or (x > 120 And y < 80) Then
    CDI = "M"

End If

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.