VBA – Operators

  • Post author:
  • Post category:VBA
  • Post comments:1 Comment
Operator

An Operator can be defined using a simple expression – 4 + 5 is equal to 9. Here, 4 and 5 are called operands and + is called operator. VBA supports the following types of operators −

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Concatenation Operators

The Arithmatic Operators

Following arithmetic operators are supported by VBA.

Assume variable A holds 5 and variable B holds 10, then −

Show Examples

OperatorDescriptionExample
+Adds the two operandsA + B will give 15
Subtracts the second operand from the firstA – B will give -5
*Multiplies both the operandsA * B will give 50
/Divides the numerator by the denominatorB / A will give 2
%Modulus operator and the remainder after an integer divisionB % A will give 0
^Exponentiation operatorB ^ A will give 100000

The Comparison Operators

There are following comparison operators supported by VBA.

Assume variable A holds 10 and variable B holds 20, then −

Show Examples

OperatorDescriptionExample
=Checks if the value of the two operands are equal or not. If yes, then the condition is true.(A = B) is False.
<>Checks if the value of the two operands are equal or not. If the values are not equal, then the condition is true.(A <> B) is True.
>Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition is true.(A > B) is False.
<Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true.(A < B) is True.
>=Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition is true.(A >= B) is False.
<=Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition is true.(A <= B) is True.

The Logical Operators

Following logical operators are supported by VBA.

Assume variable A holds 10 and variable B holds 0, then −

Show Examples

OperatorDescriptionExample
ANDCalled Logical AND operator. If both the conditions are True, then the Expression is true.a<>0 AND b<>0 is False.
ORCalled Logical OR Operator. If any of the two conditions are True, then the condition is true.a<>0 OR b<>0 is true.
NOTCalled Logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false.NOT(a<>0 OR b<>0) is false.
XORCalled Logical Exclusion. It is the combination of NOT and OR Operator. If one, and only one, of the expressions evaluates to be True, the result is True.(a<>0 XOR b<>0) is true.

The Concatenation Operators

Following Concatenation operators are supported by VBA.

Assume variable A holds 5 and variable B holds 10 then −

Show Examples

OperatorDescriptionExample
+Adds two Values as Variable. Values are NumericA + B will give 15
&Concatenates two ValuesA & B will give 510

Assume variable A = “Microsoft” and variable B = “VBScript”, then −

OperatorDescriptionExample
+Concatenates two ValuesA + B will give MicrosoftVBScript
&Concatenates two ValuesA & B will give MicrosoftVBScript

Note − Concatenation Operators can be used for both numbers and strings. The output depends on the context, if the variables hold numeric value or string value.

Next Topic:-Click Here

This Post Has One Comment

Leave a Reply