Excel Guides

Deriving an Absolute Value in a Macro in Excel

To derive the absolute value of a number in a macro in Excel, you can use the following code:

Dim myNum As Double
myNum = -5

If myNum < 0 Then
    myNum = myNum * -1
End If

Debug.Print myNum 'prints 5

This code will take a number, stored in the variable myNum, and if that number is less than zero, it will multiply it by negative one. This will effectively make the number positive, as its absolute value will now be represented. Finally, the code prints the value of myNum, which should now be five.

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.