VBA ─ Operators


VBA supports following types of operators:
  1.  Arithmetic Operators
  2.  Comparison Operators
  3.  Logical (or Relational) Operators
  4. Concatenation Operators

The Arithmetic Operators


Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition ( + ), subtraction ( - ), multiplication ( * ), and division ( / )

Operator
Description
Examples
+
Adds the two operands
A+b
-
Subtracts the second operand from the first
A-b
*
Multiplies both the operands
A*b
/
Divides the numerator by the denominator
A/b
%
Modulus operator and the remainder after an integer division
Remainder of a/b
^
Exponentiation operator
Ab


Arithmetic Operators ─ Example Add a button and try the following example to understand all the arithmetic operators available in VBA.

Sub operator_example()
 Dim a As Integer
 a = 10

 Dim b As Integer
 b = 20

 Dim c As Double

 c = a + b
 MsgBox ("Addition Result is " & c)

 c = a - b
 MsgBox ("Subtraction Result is " & c)

 c = a * b
 MsgBox ("Multiplication Result is " & c)

 c = b / a
 MsgBox ("Division Result is " & c)

 c = b Mod a
 MsgBox ("Modulus Result is " & c)

 c = b ^ a
 MsgBox ("Exponentiation Result is " & c)
End Sub


result of the example would be

Addition Result is 30

Subtraction Result is -10

Multiplication Result is 200

Division Result is 2

Modulus Result is 0

Exponentiation Result is 10240000000000






The Comparison Operators 


There are following comparison operators supported by VBA.

Comparison Operators
Description
=
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.


Try the following example to understand

Example Comparison Operators
Sub Comparison_Operators_ex()
 Dim a As Integer
 a = 10
 Dim b As Integer
 b = 15

 If a = b Then
 Debug.Print ("a is equal to b")
 Else
 Debug.Print ("a is not equal to b")
 End If

 If a <> b Then
 Debug.Print ("a is not equal to b")
 Else
 Debug.Print ("a is equal to b")
 End If

 If a > b Then
 Debug.Print ("a is Greater then b")
 Else
 Debug.Print ("a is not Greater then b")
 End If

 If a < b Then
 Debug.Print ("a is less then b")
 Else
 Debug.Print ("a is not less then b")
 End If

 If a >= b Then
 Debug.Print ("a is Greater then or equal to b")
 Else
 Debug.Print ("a is not Greater then or equal to b")
  End If

 If a <= b Then
 Debug.Print ("a is smaller then or equal to b")
 Else
 Debug.Print (" a is not smaller then or equal to b    ")
 End If

End Sub


now let see the output of the example above

Output Comparison Operators
a is not equal to b     

a is not equal to b

a is not Greater then b

a is less then b

a is not Greater then or equal to b

a is smaller then or equal to b


The Logical Operators

The concept of logical operators is simple. They allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value.
 
Operator
Description
AND
Called Logical AND operator. If both the conditions are True, then the Expression is true.
OR
Called Logical OR Operator. If any of the two conditions are True, then the condition is true.
NOT
Called Logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false.
XOR
Called 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.

The Concatenation Operators


Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and & . Both carry out the basic concatenation operation

Operator Description Example when value or variable are integer
Let values are a=8 and b = 5

Concatenation Operator
Description
Result
+
Adds two Values as Variable. Values are Numeric
A + B will give 13

&
Concatenates two Values
A & B will give 85


Operator Description Example when value or variable are string let
Let values are a= ”My” and b = ”Variable”

Concatenation Operators
Description
Result
+
Adds two Values as Variable. Values are Numeric
MyVariable

&
Concatenates two Values
MyVariable


Post a Comment

0 Comments