# If Range > Or Range < And Range >

#### 288enzo

##### Well-known Member
I couldn't find any examples of using "And" & "Or" in an If statement. Is that because you can't?

My formula is giving me an N/A, is it because of what I said above, or do I have it out of order?

VBA Code:
``If Range("W" & x) > 10 Or Range("W" & x) < -10 And Range("X" & x) > 1 Then``

Thank you

### Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
deleted

Figured it out, I had to swap them around. Go figure.

VBA Code:
``If Range("X" & x) > 1 And Range("W" & x) > 10 Or Range("W" & x) < -10 Then``

The expression will be evaluated based on operator precedence: Arithmetic operators (none in this example) before Comparison operators (you are using < and >) before logical operators (you are using And, Or).

Because comparison operators are evaluated before logical operators, you are able to write:
VBA Code:
``````If Range("W" & x) > 10 Or Range("W" & x) < -10 And Range("X" & x) > 1 Then
'rather than
If (Range("W" & x) > 10) Or (Range("W" & x) < -10) And (Range("X" & x) > 1) Then``````

There is an order of precedence for logical operators (e.g. And before Or) in the same way that there is an order of precedence for arithmetic operators (e.g. ^ before * before +).

Code:
``````'Therefore your first statement:
If Range("W" & x) > 10 Or Range("W" & x) < -10 And Range("X" & x) > 1 Then
'ís equivalent to
If Range("W" & x) > 10 Or (Range("W" & x) < -10 And Range("X" & x) > 1) Then

If Range("X" & x) > 1 And Range("W" & x) > 10 Or Range("W" & x) < -10 Then
'ís equivalent to
If (Range("X" & x) > 1 And Range("W" & x) > 10) Or Range("W" & x) < -10 Then``````

There is a third possibility which I am guessing might be the one you actually want?
Code:
``````If Range("X" & x).Value > 1 And (Range("W" & x).Value > 10 Or Range("W" & x).Value < -10) Then
'which you could also write
If Range("X" & x).Value > 1 And Abs(Range("W" & x).Value > 10) Then``````

Replies
9
Views
223
Replies
5
Views
120
Replies
5
Views
160
Replies
5
Views
198
Replies
0
Views
138

1,202,983
Messages
6,052,907
Members
444,611
Latest member
ggwpnore

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back