Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number.

Syntax

Sgn (Number)

Parameter

Number (required) –This parameter represents the number that you want the arithmetic sign of.

Return

This function returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number.

Example 1

Sub SgnFunction_Example1()
 ' It will return the arithmetic sign of the number.
 Dim sgn_int As Integer
 sgn_int = Sgn(15)
 ' The variable sgn_int will return 1
 Cells(1, 1).Value = sgn_int
 End Sub 

Output

1

VBA Sgn Function

Example 2

Sub SgnFunction_Example2()
 ' It will return the arithmetic sign of the number.
 Dim sgn_int As Integer
 'for negative numbers
 sgn_int = Sgn(-15)
 ' The variable sgn_int will return -1
 Cells(1, 1).Value = sgn_int
 End Sub 

Output

-1

VBA Sgn Function

Example 3

Sub SgnFunction_Example3()
 ' It will return the arithmetic sign of the number.
 Dim sgn_int As Integer
 'for number equal to zero
 sgn_int = Sgn(0)
 ' The variable sgn_int will return 0
 Cells(1, 1).Value = sgn_int
 End Sub 

Output

0

VBA Sgn Function