VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number.
Syntax
1 2 3 |
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
1 2 3 4 5 6 7 8 9 |
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
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 8 9 10 |
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
1 2 3 4 5 6 7 8 9 10 |
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
