Can I use AND in macro IF statement?

Can I use AND, OR in macro if statements like we can in other languages -

for example in python we can use && operator like
If(condA =true && condB = true)
–code here

We can use it in the spreadsheet but can we use it the Macro Basic? if yes, please give one example too, thank you. Other Than nested IF statements

Hello,

Here is a simple example. Note you must be careful on grouping conditions:

Sub ifTest
a = 2
b = 6
    if (a = 2 or 3) and (b > 5) then
      MsgBox "Yes"
    Else
      MsgBox "No"
    End if
End sub

There are many reference manuals available on-line. Here is a good one → OpenOfice Macros Explained

Also, that is not a Python statement you present. See → Python If … Else for example.

a = 2 or 3 might give not what you expect :wink:

Thanks, It worked

@mikekaganski,

You are absolutely correct :slight_smile:

Pretty bad mistake on my part :frowning:

… and below I also made a mistake, not taking operation precedence rules of Basic myself :slight_smile:

(a = 2) or (a = 3)

Sub ifTest
    a = 4
    b = 6
        if (a = 2) or (a = 3) and (b > 5) then
          MsgBox "Yes"
        Else
          MsgBox "No"
        End if
    End sub

(a = 2) or (a = 3) and (b > 5) might give not what you expect :wink:

For the above function it works.

Agreed, in Basic AND and OR have the same precedence, and are evaluated left-to-right.

In Basic NOT, AND, OR, XOR are operators named with respect to their meaning if used with Boolean operands. They can also be used (for masking purposes e.g.) with integer (Long) operands. They are applied bit-by-bit in this case. Thus you get 5 OR -7 = -3 e.g.

As known from ordinary algbra the operators are “prioritized” in a specific way. In case of the logical operators the NOT binds closest to the single operand at its right side. AND has highest príority among the two-sided operands like the * in arithmetic. OR has less binding force like the + , and concerning the XOR, I’m not sure.

You see, I have to admit that doubt may occur, this the more if integer expressions come into account. Therefore my advice: If you not are completely sure about the way priorities rule your expressions, disambiguate everything with the help of parentheses.