Using multiple inequalities in an if statements

Nauticulus

New Member
Joined
Apr 16, 2018
Messages
1
Hi,

I'm trying to check two cell values where both must be within the same range like this...

If 1<=Cell a<=6 and 1<=Cell b<=6 then execute

This is what I have at the moment...
Code:
If ws2.Cells(d, "B").Value <= 6 And => 1 And ws2.Cells(d, "C").Value <= 7 And => 1 Then
Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to the Board!

Each part of it must be a complete statement, i.e.
Code:
If ws2.Cells(d, "B").Value <= 6 And ws2.Cells(d, "B").Value >= 1 And ws2.Cells(d, "C").Value <= 7 And ws2.Cells(d, "C").Value >= 1 Then
To me, this can sometimes be confusing to look at, so I add parentheses:
Code:
If (ws2.Cells(d, "B").Value <= 6) And (ws2.Cells(d, "B").Value >= 1) And (ws2.Cells(d, "C").Value <= 7) And (ws2.Cells(d, "C").Value >= 1) Then
 
Last edited:
Upvote 0
Welcome to the Board!

Each part of it must be a complete statement, i.e.
Code:
If ws2.Cells(d, "B").Value <= 6 And ws2.Cells(d, "B").Value >= 1 And ws2.Cells(d, "C").Value <= 7 And ws2.Cells(d, "C").Value >= 1 Then
To me, this can sometimes be confusing to look at, so I add parentheses:
Code:
If (ws2.Cells(d, "B").Value <= 6) And (ws2.Cells(d, "B").Value >= 1) And (ws2.Cells(d, "C").Value <= 7) And (ws2.Cells(d, "C").Value >= 1) Then
This is a personal preference, but I think it helps make things clear when and if the code ever has to be revisited in the future... I like to arrange the logical operations so that natural looking "ranges" (1<=x and x<=6) are displayed. Given that, here is how I would rearrange your second code line (I fully agree with you about the parentheses)...
Code:
[table="width: 500"]
[tr]
	[td]If ((1 <= ws2.Cells(d, "B").Value) And (ws2.Cells(d, "B").Value <= 6)) And ((1 <= ws2.Cells(d, "C").Value) And (ws2.Cells(d, "C").Value <= 7)) Then[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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