Logical Operators

Logical operators evaluate expressions and return a logical value of True or False. For example, you can use a logical operator to compare two comparison expressions.

OPERATOR

PURPOSE

Not

Negates the value of the expression. If the expression is True the operator causes it to be false, or vice versa.

And

Performs a logical conjunction of two expressions. If they are both True, the result is True. If either of the expressions is False the result is False. If either expression is Null the result is Null.

Or

Performs a logical disjunction of two expressions. If the value of either expression is True, the result is True; otherwise, the result is False. Just like the And operator, if either expression is Null, the result is also Null.

Xor

Performs a logical exclusions (exclusive or) on two expressions. The result is the converse of the Eqv operator. If both expressions are True or if both are False the result is False. If one expression is True and the other is False the result is True.

Eqv

Performs a logical equivalence on two expressions. If both expressions are True or if both are False, the result is True; otherwise the result is False.

Imp

Performs a logical implication on two expressions. If both expressions are True or if both are False, the result is True. If the first is True and the second is False, the result is False, but if the first is False and the second is True the result is True.

OPERATOR

PURPOSE

=

Determines if expressions are equal.

>

Determines if first expression is greater than second expression.

<

Determines if first expression is less than the second expression.

<>

Determines if expressions are not equal.

> =

Determines if first expression is greater than or equal to second expression.

< =

Determines if first expression is less than or equal to second expression.

With this expression, the If statement can execute only if both expressions are true.

VBA supports six different logical expressions.

The following table lists Logical Operators:

CREATE A SUBROUTINE

You can easily create a subroutine within the Visual

Basic Editor that executes a series of VBA commands.

Each macro that runs in Excel is actually just a subroutine that contains blocks of VBA code. That said, a single subroutine can call other subroutines and functions, creating a macro that is much more complex than just a simple subroutine.

VBA provides essentially two different types of subroutines: private and public. When you create a macro with the Macro Recorder, the subroutine it creates is public, meaning that all procedures, including the Macro dialog box, can access and see it. Conversely, only other procedures within the same module can access a private subroutine. Excel hides all Private subroutines from the Macro dialog box and you cannot activate them with key combinations. You should mark subroutines as private if you do not want them accessible as macros. You mark a subroutine as private by placing Private before the Sub statement, for example: Private Sub SampleSub(). Typically, other subroutines within the same module call private subroutines. A subroutine is called using the Call statement: Call SampleSub(). Excel considers any subroutines that do not have the Private keyword to be public. That being said, the use of the Public keyword is really unnecessary because a subroutine with no keyword is the same as one with the Public keyword.

VBA does allow a subroutine to be called without the Call statement. Even though VBA does not require it, you should always use the Call statement to remind you that another procedure is being called. Using the Call statement makes your code much more readable because another user can quickly look at the code and see that another subroutine is being called.

CREATE A SUBROUTINE

CREATE A SUBROUTINE

Logical Operators Vba

-D In the Project window, click the project where you want to add a new module.

Type Sub.

"H Type the name of your subroutine.

-D In the Project window, click the project where you want to add a new module.

window.

Type Sub.

"H Type the name of your subroutine.

Introduction to VBA" for information about naming subroutines.

0 0

Responses

  • CORNELIA
    Which statement's remind the programmer what each subroutine does?
    2 years ago
  • luke
    How to compare two columns using logical operator in VBA?
    4 months ago

Post a comment