VBA SGN Function (Syntax + Example)

puneet-gogia-excel-champs-09-06-23

- by Puneet

The VBA SGN function is listed under the math category of VBA functions. When you use it in a VBA code, it returns the arithmetic sign of an integer. In simple words, it returns +1 for a positive number, -1 for a negative number, and 0 for a zero.

Syntax

Sgn(Number)

Arguments

  • Number: An integer to specify the number that you want the arithmetic sign of.

Example

To practically understand how to use the VBA SGN function, you need to go through the below example where we have written a vba code by using it:

example-vba-sgn-function
Sub example_SGN()
Range("B1").Value = Sgn(Range("A1"))
End Sub

In the above code, we have used the SGN to get the arithmetic sign and it has returned 1 as we have a positive number in cell A1.

Notes

  • If the value specified is a value other than a number or a number that can’t be recognized as a number, VBA will return the run-time 13 error.