Using VBA's Mathematical Functions
VBA provides a solid suite of functions for standard mathematical operations. Table below lists these functions with examples.
VBA's mathematical functionsAbs(number) | The absolute value of number - the unsigned magnitude of the number. | Abs(-100) returns 100. |
Atn(number) | The arctangent of number in radians. | Atn(dblMyAngle) |
Cos(number) | The cosine of angle number. | Cos(dblMyAngle) |
Exp(number) | e, the base of natural logarithms, raised to the power of number. | Exp(5) returns 148.413159102577. |
Fix(number) | The integer portion of number (without rounding). If number is negative, returns the negative number greater than or equal to number. | Fix(3.14159) returns 3. Fix(-3.14159) returns -3. |
Int(number) | The integer portion of number (again, without rounding). If number is negative, returns the negative number less than or equal to number. | Int(3.14159) returns 3. Int(-3.14159) returns -4 . |
Log(number) | The natural logarithm of number. | Log(dblMyAngle) |
Rnd([number]) | A random number (with no argument) or a number based on the given initial seed. | Rnd(1) returns a random number. |
Sgn(number) | -1 if number is negative, 0 if number is 0, 1 if number is positive. | Sgn(7) returns 1. Sgn(-7) returns -1. Sgn(0) returns 0. |
Sin(number) | The sine of the angle specified by number (measured in radians). | Sin(dblMyAngle) |
Sqr(number) | The square root of number. If number is negative, VBA gives a runtime error. | Sqr(9) returns 3. |
Tan(number) | The tangent of the angle specified by number (measured in radians). | Tan(dblMyAngle) |
In this tutorial:
- Using Built-in Functions
- What Is a Function?
- Passing Arguments to a Function
- Using Functions to Convert Data from One Type to Another
- Using the Asc Function to Return a Character Code
- Using the Str Function to Convert a Value to a String
- Using the Format Function to Format an Expression
- Using the Chr Function and Constants to Enter Special Characters in a String
- Using Functions to Manipulate Strings
- Using InStr and InStrRev to Find a String within Another String
- Using LTrim, RTrim, and Trim to Trim Spaces from a String
- Using Len to Check the Length of a String
- Using the StrComp Function to Compare Apples to Apples
- Using VBA's Mathematical Functions
- Excel VBA's Date and Time Functions
- Using the DateDiff Function to Return an Interval
- Using File-Management Functions