# Perform Mathematical Calculations

I d ou can perform many types of mathematical calcuations within your macros. VBA provides several different operators for performing mathematical calculations within your procedures. Because Excel typically contains numeric values, you frequently use VBA operators to create Excel macros.

VBA includes eight different arithmetic operators for performing calculations. These operators include + (addition), - (subtraction), A (exponential), * (multiplication), / (division), \ (integer division), and MOD (Modulo - return remainder).

You typically use operators to perform a mathematical operation on a specific variable. For example, you frequently use the + operator with a For Next loop to increment the loop counter variable. For example, you can read the statement i = i + 1 to mean: "Take the value of the variable i, add 1 to it, and place the result back in the variable." You frequently encounter this type of mathematical calculation in source code for all programming languages.

VBA provides three different operators that deal with dividing one value by another: /, \, and MOD. Each of these operators returns a different type of value. The /, or division, operator divides two values and returns the entire result, including any decimal portion that results when the numbers do not divide evenly. The \, or integer division, operator divides two values and returns only the integer portion of the result. Any remainder is discarded with this operator. Finally, the MOD operator divides two numbers and returns only the remainder. This operator works well for predetermining if two numeric values divide evenly. If a zero is returned, the values divided evenly and no remainder exists.

See "Arithmetic Operators" in the section "An Introduction to VBA" for more information about the available operators and the precedence order in which calculations take place.

PERFORM MATHEMATICAL CALCULATIONS

PERFORM MATHEMATICAL CALCULATIONS

B-Ö Microsoft Excel Qb|e_^J 1-0 Sheetl (Sheen) —® Sheet2 (5heet2) ■ H] Sheet3 (Sheet3) I

-Ps Modules

Modulel

-Ä Modules I VBAProject (OFficeEKi

3-Q Microsoft Excel Obje L® si

Module2 Module Alphabetic | Categorized |

'-H Create a subroutine.

B Declare two variables as numeric data types.

B-Ö Microsoft Excel Qb|e_^J 1-0 Sheetl (Sheen) —® Sheet2 (5heet2) ■ H] Sheet3 (Sheet3) I

-Ps Modules

Modulel

-Ä Modules I VBAProject (OFficeEKi

3-Q Microsoft Excel Obje L® si inif1

Module2 Module Alphabetic | Categorized |

Diuide_Values

Sub Divide_Values0 Dim Varl As Integer Dim Var2 As Integer

'-H Create a subroutine.

+1 0

### Responses

• delma
What is performing mathematical calculations in programming?
3 years ago