In this lesson, you'll learn how to work with Excel VBA's trigonometric functions including Sin, Cos, Tan and Atn, with practical examples and proper angle conversion techniques.
Excel VBA provides these key trigonometric functions:
Since VBA uses radians, we convert degrees to radians using:
π radians = 180°
therefore
1° = π/180 radians
To get the precise value of π in VBA:
pi = 4 * Atn(1) ' Most accurate way to get π
The Sin function returns the sine of an angle in radians.
Syntax:
Sin(angle_in_radians)
Private Sub CommandButton1_Click() Dim pi As Single pi = 4 * Atn(1) MsgBox "Sin(90°) is " & Round(Sin(pi/2), 4) End Sub
Running the program produces this message:
The Cos function returns the cosine of an angle in radians.
Syntax:
Cos(angle_in_radians)
Private Sub CommandButton1_Click() Dim pi As Single pi = 4 * Atn(1) MsgBox "Cos(60°) is " & Round(Cos(pi/3), 4) End Sub
Running the program produces this message:
The Tan function returns the tangent of an angle in radians.
Syntax:
Tan(angle_in_radians)
Private Sub CommandButton1_Click() Dim pi As Single pi = 4 * Atn(1) MsgBox "Tan(45°) is " & Round(Tan(pi/4), 4) End Sub
Running the program produces this message:
These trigonometric functions are fundamental for engineering, physics, and geometry applications in Excel VBA.
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page