ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Having "Or" in If statements in VB doesn't work? (https://www.excelbanter.com/excel-programming/391396-having-if-statements-vbulletin-doesnt-work.html)

J@Y

Having "Or" in If statements in VB doesn't work?
 
I was trying this statement:

If a = 2 or b =2 then .....

But the b = 2 doesn't get tested for some reason, why is that?


Don Guillett

Having "Or" in If statements in VB doesn't work?
 

You might want to tell us what a & b are.

--
Don Guillett
SalesAid Software

"J@Y" wrote in message
...
I was trying this statement:

If a = 2 or b =2 then .....

But the b = 2 doesn't get tested for some reason, why is that?



Pops Jackson

Having "Or" in If statements in VB doesn't work?
 
I would separate the line as follows:

If a = 2 then
blah blah blah
elseif b = 2 then
blah blah blah
End If

Or, if you want to test for both equalling 2:
If a = 2 then
If b = 2 then
etc.
--
Pops Jackson


"J@Y" wrote:

I was trying this statement:

If a = 2 or b =2 then .....

But the b = 2 doesn't get tested for some reason, why is that?


Mark Lincoln

Having "Or" in If statements in VB doesn't work?
 
On Jun 15, 11:27 am, J@Y wrote:
I was trying this statement:

If a = 2 or b =2 then .....

But the b = 2 doesn't get tested for some reason, why is that?


If a = 2, the Or expression will evaluate to True regardless of the
value of b. If you require both variables to be equal to 2 to get a
result of True you need to use And rather than Or:

If a = 2 and b = 2 then [...]


JE McGimpsey

Having "Or" in If statements in VB doesn't work?
 
It isn't, at least for me:

Dim a As Long
Dim b As Long

a = 0
b = 0
If a = 2 Or b = 2 Then MsgBox "a: " & a & " b: " & b
a = 2
b = 0
If a = 2 Or b = 2 Then MsgBox "a: " & a & " b: " & b
a = 0
b = 2
If a = 2 Or b = 2 Then MsgBox "a: " & a & " b: " & b



In article ,
J@Y wrote:

I was trying this statement:

If a = 2 or b =2 then .....

But the b = 2 doesn't get tested for some reason, why is that?



All times are GMT +1. The time now is 03:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com