If statement with AND and two OR

mark692

Active Member
Joined
Feb 27, 2015
Messages
321
Office Version
  1. 2016
Platform
  1. Windows
Hi guys i have if statement with one AND and two OR it seems not working can you guys check my code. thank you :)

This is the scenario if A1 is greater than B1 and C1 value is not equal to Withdrawals or Inventory then select petty cash sheet.
If Range("A1").Value > Range("B1").Value And (Range("C1").Value <> "Withdrawals" Or Range("C1").Value <> "Inventory") Then

Sheets("Petty Cash Data").Select
Else

End If
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I think you have your logic messed up.
I think the OR here:
Rich (BB code):
(Range("C1").Value <> "Withdrawals" Or Range("C1").Value <> "Inventory")
needs to be an AND:
Rich (BB code):
(Range("C1").Value <> "Withdrawals" And Range("C1").Value <> "Inventory")
as your original statement will ALWAYS be true here, since it is impossible to be BOTH "Withdrawals" and "Inventory" at the same time.
(Note that when using "<>", you want to switch the OR to an AND. Of use were using and "=" expression, then it makes sense to use OR).
 
Upvote 0
this is to simpify my formula sir

A1 > B2 and ( C2 not "inventory" or C2 not "Withdrawals) then dont select "petty cash data"
 
Upvote 0
this is to simpify my formula sir

A1 > B2 and ( C2 not "inventory" or C2 not "Withdrawals) then dont select "petty cash data"
I understand what you are trying to do.
Did you at least TRY my suggestion???
Before commenting on it, try it and see what happens.

Also, either you made a typo in your last post, or you seem to be changing what you are trying to do:

Original:
if A1 is greater than B1 and C1 value is not equal to Withdrawals or Inventory then select petty cash sheet.
last post:
A1 > B2 and ( C2 not "inventory" or C2 not "Withdrawals) then dont select "petty cash data"
 
Upvote 0
This has been a confusing point also for me since I started programming.

Simply, "..is different than something.." means "..is Not equal to..". However, "Or" statements play differently with "Not" statements. They act like the opposite. I know it does't sound like it does to the ear but you must say:
VBA Code:
If Range("A1").Value > Range("B1").Value And (Range("C1").Value <> "Withdrawals" And Range("C1").Value <> "Inventory") Then
 
Upvote 0
I understand what you are trying to do.
Did you at least TRY my suggestion???
Before commenting on it, try it and see what happens.

Also, either you made a typo in your last post, or you seem to be changing what you are trying to do:

Original:

last post:
This has been a confusing point also for me since I started programming.

Simply, "..is different than something.." means "..is Not equal to..". However, "Or" statements play differently with "Not" statements. They act like the opposite. I know it does't sound like it does to the ear but you must say:
VBA Code:
If Range("A1").Value > Range("B1").Value And (Range("C1").Value <> "Withdrawals" And Range("C1").Value <> "Inventory") Then

ohh i get it now thank you :) i will try it
 
Upvote 0
This has been a confusing point also for me since I started programming.

Simply, "..is different than something.." means "..is Not equal to..". However, "Or" statements play differently with "Not" statements. They act like the opposite. I know it does't sound like it does to the ear but you must say:
VBA Code:
If Range("A1").Value > Range("B1").Value And (Range("C1").Value <> "Withdrawals" And Range("C1").Value <> "Inventory") Then
I always find it makes more sense if you actually break it down and work through an example.
Remember, with an "AND", BOTH statements must be TRUE in order to return TRUE.
For an "OR", only one needs to be TRUE in order to return TRUE.

Let's evaluate the possible situations for this statement.
First part: Range("C1").Value <> "Withdrawals"
Second part: Range("C1").Value <> "Inventory"

Here is a grid that shows the possible different values, and what they return using OR vs. AND:
Test ValueFirst partSecond partOR returnsAND returns
"Withdrawals"
FALSE​
TRUE​
TRUE​
FALSE​
"Inventory"
TRUE​
FALSE​
TRUE​
FALSE​
"Dog"
TRUE​
TRUE​
TRUE​
TRUE​

As you can see, your original statement will ALWAYS evaluate to TRUE. It is impossible to ever return FALSE, the way you wrote it.
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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