Help using AND OR in IIF statement

oliviar

Board Regular
Joined
Sep 12, 2010
Messages
184
Hi Guys,
I want to do this:
iif([POP] = 1 OR 2 OR 3,[POP]+1,[POP])

When I wrote it like that, it just added 1 to everything - even if the POP wasn't 1,2,or 3.

What am I doing wrong?
:(
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Maybe:
IIf( ([POP] = 1) OR ([POP] = 2) OR ([POP] = 3), [POP]+1, [POP])
 
Upvote 0
Hi Guys,
I want to do this:
iif([POP] = 1 OR 2 OR 3,[POP]+1,[POP])

When I wrote it like that, it just added 1 to everything - even if the POP wasn't 1,2,or 3.

What am I doing wrong?
:(


The IIf statement's syntax is IIf(expression, Truepart, Falsepart). It evaluates the expression and if it evaluates to True, it returns the Truepart, otherwise the Falsepart. When you wrote "[POP] = 1 OR 2 OR 3" for the expression you really wrote "([POP] = 1) OR (2) OR (3)". The (2) and (3) will always evaluate to True since the expression 2 is the same as (2=2). This is the same for any number left alone like that. Since you're using OR any part of that expression that evaluates to True will cause the IIf to return the Truepart, thus it will always add 1 to everything as you found.

Xenou's method will get around this.

hth,

Rich
 
Upvote 0
I'm sorry, I still don't understand why the language would do POP+1 if POP=5 in my original version.
:(

I want to get inside its head and understand how it works....
?
 
Upvote 0
everything in the OR clause must evaluate to TRUE or FALSE.

If [POP] is 5 then all of these are FALSE:
Code:
[POP] = 1
[POP] = 2
[POP] = 3
But if you write this:
Code:
(1 OR 2 OR 3)

The answer is always TRUE. 1 is TRUE. 2 is TRUE. 3 is TRUE. Anything but 0 is always TRUE.
 
Upvote 0
hmmm. I still don't understand how ([5]=1 or 2 or 3) is true, but ([5]=0) is false, but I am probably annoying you know and will just accept it as a quirk of VB.
:confused:
 
Upvote 0
The situation is that you need to provide the ENTIRE comparison for each value and not just, as you have found out, the first one. The other option depending on what you have for other options is to use this to shorten it:
Code:
IIf([POP]>=1 OR [POP]<=3, [POP] + 1, [POP])
 
Upvote 0
Another way of writing your incorrect statement is:
Code:
x = 5
If Not x = 1 Then
    If 2 Then
        If 3 Then
            x = 5 + 1
        Else
            x
        End If
    End If
End If

Another way of writing the correct statement is:
Code:
x = 5
If Not x = 1 Then
    If Not x = 2 Then
        If Not x = 3 Then
            x = 5 + 1
        Else
            x
        End If
    End If
End If
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

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