Javatpoint Logo
Javatpoint Logo

VBA Logical operator

You can have more than one condition in the same line of your If and else If statements. To test for more than one condition, you need the logical operators.

Below are the following logical operators that supported by VBA:

1. AND: AND logical operator is used to combining more than one condition. If all the conditions are True AND return the True. If any of the condition is False AND returns the False.

For example, You want to check if a number is greater than 10 but less than 20, then you can use the AND operator to test this condition.

In the above example, we have used the variable Age only once. But you need to type the variable twice, once before the AND operator and once after the AND operator.

Only if both conditions are True, then the entire line becomes True. And if one of them is False, then the whole line becomes False.

2. OR: OR operator is used to combining more than one condition. If any of the one condition will be True, it returns the True, and if all the False it returns the False.

For example, Create a new Sub in the code window.

The code sets up two integer variables, x, and y. We have a value of 10 in x and a value of 20 in y. The IF Statement is trying to test what's in these numbers:

If x = 10 Or y = 20 Then

The valid numbers are 10 and 20. We don't care if x AND y hold 10 and 20. as long as at least one of them holds the correct number, then that's OK.

Run the code, and you will see the first message box display, "Valid Number". Now change your code so that x holds a value of 30.

Rerun the program, and you'll find the first message box still displays. Then, change the value of y to 40, and now neither number holds the correct value. In this case, the second message box will display when the program is run.

3. NOT: NOT operator works as an inverse function. If the condition id True, it returns False. And if the condition is False, it returns True.

For example, Create a new sub in the code window, and use the Boolean Flag variable.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA