This post will describe the VBA operators in the MS Excel. There are 4 kinds of operators in VBA as bellow:
- Arithmetic operators
- Comparison operators
- Concatenation operators
- Logical operators
Table of Contents
VBA Arithmetic Operators
Arithmetic operators used to perform the basic mathematical operations such as: addition, subtraction, multiplication, or division and so on. Let see the below table and contains all operators in Excel VBA.
Arithmetic operator | Description | Example |
+ (plus sign) | Addition | 3+3 |
– (minus sign) | Subtraction Negation |
3–1 –1 |
* (asterisk) | Multiplication | 3*3 |
/ (forward slash) | Division | 3/3 |
% (percent sign) | Percent | 20% |
^ (caret) | Exponentiation | 3^2 |
VBA Comparison Operators
The comparison operators used to compare two values and the result will be returned, such as: True or False.
The following comparison operators can be used in the VBA:
Comparison operator | Description | Example(A1=10, B1=15) |
= (equal sign) | Equal to | A1=B1 is False. |
> (greater than sign) | Greater than | A1>B1 is False |
< (less than sign) | Less than | A1<B1 is True |
>= (greater than or equal to sign) | Greater than or equal to | A1>=B1 is False |
<= (less than or equal to sign) | Less than or equal to | A1<=B1 is True |
<> (not equal to sign) | Not equal to | A1<>B1 is True |
VBA Concatenation Operators
The concatenation operators use to join two or more text strings to produce one continuous text string. Or adds two values as variable, and those two values are numeric.
Text operator | Description | Example(A1=”excel”, B1=”how”) |
& (ampersand) | Connects, or concatenates, two values to produce one continuous text value | A1&B1 is “excelhow” |
Text operator | Description | Example(A1=”excel”, B1=”how” A2=10 B2 =15) |
+ | If the values are numeric and it will perform the addition operation. Or if the values are two text string, then it will concatenate those tow text string. | A1+B1 will give “excelhow”
A2 +B2 will give 25 |
VBA Logical Operators
The following Three logical operators are the most useful in the Excel VBA:
Logical operator | Description | Example (A1=15, B1=0) |
AND | Logical AND operator. If both the conditions are True, then the Expression is true. | A1>0 AND B1<>0 is False. |
OR | Logical OR Operator. If any of the two conditions are True, then the condition is true. | A1<>0 OR B1<>0 is true. |
NOT | 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(A1<>0 OR B1<>0) is false. |
Leave a Reply
You must be logged in to post a comment.