VBA – Arithmetic Operators

  • Post author:
  • Post category:VBA
  • Post comments:1 Comment
arithmetic operators

Following arithmetic operators are supported by VBA.

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

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

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

Previous Page:-Click Here

This Post Has One Comment

Leave a Reply