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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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.
 
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 [...]
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top