VBA - Arithmetic Operators



Following arithmetic operators are supported by VBA.

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

Operator Description Example
+ Adds the two operands A + B will give 15
- Subtracts the second operand from the first A - B will give -5
* Multiplies both the operands A * B will give 50
/ Divides the numerator by the denominator B / A will give 2
% Modulus operator and the remainder after an integer division B % A will give 0
^ Exponentiation operator B ^ A will give 100000

Example

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

Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 5
   
   Dim b As Integer
   b = 10
   
   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

When you click the button or execute the above script, it will produce the following result.

Addition Result is 15

Subtraction Result is -5

Multiplication Result is 50

Division Result is 2

Modulus Result is 0

Exponentiation Result is 100000
vba_operators.htm
Advertisements