VBA Arithmetic Operators: Multiplication, Division, Addition, Modulus

VBA Arithmetic Operators are used to perform arithmetic operations such as adding, subtracting, dividing or multiplying numbers.

The following table shows the arithmetic operators in VBA

1+Addition: This operator is used to add up numbers2 + 24
2Subtraction: This operator is used to subtract numbers5 – 32
3*Multiplication: This operator is used to multiply numbers3 * 26
4/Division: This operator is used to divide numbers9 / 33
5^Exponentiation: This operator is used to raise a number to the power of another number2^38
6modModulus Operator: Divides a number and returns the remainder10 mod 31

VBA Arithmetic Operators: Modulus, Multiplication, Division, Addition

VBA Arithmetic Operators Example

Add a button to the Excel sheet as we show earlier, and then follow the following points

  • Change the name property to btnAdd
  • Change the caption property to Add Operator
  • Right click on the button
  • Select view code
  • You will get the following code window

Enter the following code in between Private Sub btnAdd_Click() and End Sub

  • Dim x As Integer, z As Integer
    • x = 2
    • z = 3
  • MsgBox x + z, vbOKOnly, “Addition Operator”
  • Click on save button
  • Close the code editor window

Let’s now execute our code

On the ribbon bar, look for the button Design Mode

If the button is in active state (green background colour), then it’s in design mode. You cannot execute code in this stateIf it is not in the active state (white background color), then it allows you to run the code.

Click on Design Mode button

The button should now appear as follows

Click on Add operator

You will get the following results

Download the above Excel Code

Arithmetic Operators Tutorial exercise

The best way to learn is by practicing. Follow the above steps to create buttons for subtraction, division, multiplication and exponentiation.

Write the code for the buttons and test them to see if the code executes.

Source: https://www.guru99.com/vba-arithmetic-operators.html

Be the first to comment

Leave a Reply

Your email address will not be published.