ExcelFunctions.net

Search Site:

The VBA Tan function returns the tangent of a supplied angle.

The syntax of the function is:

Tan( Number )

Where the supplied Number is the angle (in radians) that you want to calculate the tangent of.

In the following example, the VBA Tan function is used to return the Tangent of three different angles (which are expressed in radians).

' Calculate the tangent of three different angles (supplied in radians). Dim val1 As Double Dim val2 As Double Dim val3 As Double val1 = Tan( -1.0471975511966 ) ' The variable val1 is now equal to -1.73205080756889. val2 = Tan( 0 ) val3 = Tan( 0.523598775598299 )' The variable val2 is now equal to 0. ' The variable val3 is now equal to 0.577350269189626. |

In the above VBA code:

- For the angle -1.0471975511966 (-π/3), the Tan function returns the value
*-1.73205080756889*; - For the angle 0, the Tan function returns the value
*0*; - For the angle 0.523598775598299 (π/6), the Tan function returns the value
*0.577350269189626*.

If the angle that you want to calculate the tangent of, is expressed in degrees, this must be converted into radians, by multiplying by π/180, before it is supplied to the Tan function. An example of this is provided below:

' Calculate the tangent of 30 degrees. Const pi = 3.14159265358979 ' Convert 30 degrees to radians by multiplying by pi/180.Dim val1 As Double val1 = Tan( 30 * pi / 180 ) ' The variable val1 is now equal to 0.577350269189625. |

In the above VBA code, the angle 30 degrees is converted to radians before it is supplied to the Tan function.

The VBA Tan function then returns the value *0.577350269189625*.

Note that, in order to clarify the code, the value 3.14159265358979 (π) has been assigned to the constant, pi, which is used in the calculation.

If the Tan function is supplied with a value that cannot be interpreted as a number, it will return the error:

Run-time error '13': Type mismatch