If Range > Or Range < And Range >

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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
 
Upvote 0
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

'... and your second statement:
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
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top