ExcelFunctions.net

Search Site:

The VBA Sin function returns the sine of a supplied angle.

The syntax of the function is:

Sin( Number )

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

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

' Calculate the sine of three different angles (supplied in radians).
Dim val1 As Double
Dim val2 As Double Dim val3 As Double
val1 = Sin( -1.5707963267949 )
' The variable val1 is now equal to -1.
val2 = Sin( 0 )
val3 = Sin( 1.0471975511966 )' The variable val2 is now equal to 0. ' The variable val3 is now equal to 0.86602540378444. |

In the above VBA code:

- For the angle -1.5707963267949 (-π/2), the Sin function returns the value
*-1*; - For the angle 0, the Sin function returns the value
*0*; - For the angle 1.0471975511966 (π/3), the Sin function returns the value
*0.86602540378444*.

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

' Calculate the sine of 60 degrees.
Const pi = 3.14159265358979
' Convert 60 degrees to radians by multiplying by pi/180.Dim val1 As Double val1 = Sin( 60 * pi / 180 ) ' The variable val1 is now equal to 0.866025403784438. |

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

The VBA Sin function then returns the value *0.866025403784438*.

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 Sin function is supplied with a value that cannot be interpreted as a number, it will return the error:

Run-time error '13': Type mismatch