# How to Use VBA ABS Function in Excel

Let's say you have a -5 in a cell so when you write a code with ABS function it will convert that number into 5.

An absolute number is always far from zero.

Abs(Number)

## Example

In the below example, you have a negative number in cell A1. Here you can write a code like below to convert this number into an absolute number.

Sub absNumber()

Range("A1").Value = Abs(Range("A1"))

End Sub

When you run the above code, it will replace the -5 with 5. Here 5 is an absolute number. If you want to convert all the numbers from selected cells into absolute numbers, you can use below code.

Sub absNumber()

Dim rng As Range

For Each rng In Selection

rng = Abs(rng)

Next rng

End Sub

Please not one thing that if a cell has a value other than number then ABS function returns an "Type Mismatch" error. So, for this you can use a code like below.

Sub absNumber()

Dim rng As Range

For Each rng In Selection

If IsNumeric(rng) = True Then

rng = Abs(rng)

End If

Next rng

End Sub

Related Tips to Learn Next