Excel VBA ABS Function
VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number.
Syntax
Abs (Number)
Parameter
Number (required) – This parameter represents the number that you want the absolute value of.
Return
This function returns the absolute value of a number.
Example 1
Sub AbsFunction_Example1() ' Returning the absolute value of the specified number Dim abs_val As Double abs_val = Abs(15) ' The integer abs_val will return 15 Cells(1, 1).Value = abs_val End Sub
Output
15
Example 2
Sub AbsFunction_Example2() ' Returning the absolute value of the specified number Dim abs_val As Double 'with negative number abs_val = Abs(-15) ' The integer abs_val will return positive 15, irrespective of negative sign Cells(1, 1).Value = abs_val End Sub
Output
15
Example 3
Sub AbsFunction_Example3() ' Returning the absolute value of the specified number Dim abs_val As Double 'with decimal value abs_val = Abs(1.005) ' The integer abs_val will return positive 1.005 Cells(1, 1).Value = abs_val End Sub
Output
1.005
Example 4
Sub AbsFunction_Example4() ' Returning the absolute value of the specified number Dim abs_val As Double 'with string abs_val = Abs("Hey") ' The integer abs_val will return type mismatch error Cells(1, 1).Value = abs_val End Sub
Output