See Also Example
Returns the absolute value of a number.
The argument number can be any valid numeric expression The absolute value of a number is its unsigned magnitude. For example, .ABS(-1) and ABS(1) both return 1.
The data type3GYXY7 of the return value is the same as that of the number argument. However, if number is a Variant of VarType 8 (String) and can be converted to a number, the return value will be a Variant of VarType 5 (Double). If the numeric expression results in a Null , Abs returns a Null.
Abs Function Example
The example finds the approximate value for a cube root. It uses Abs to determine the absolute difference between two numbers. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg, Precision, Value, X, X1, X2 ' Declare variables.
Precision = .00000000000001
Value = InputBox("Enter a value: ") ' Prompt for input.
X1 = 0: X2 = Value ' Make first two guesses.
' Loop until difference between guesses is less than precision.
Do Until Abs(X1 - X2) < Precision
X = (X1 + X2) / 2
If X * X * X - Value < 0 Then ' Adjust guesses.
X1 = X
X2 = X
Msg = "The cube root of " & Value & " is "
Msg = Msg & X & "."
MsgBox Msg ' Display message.