Excel VBA Lesson 7: Trigonometric Functions in Excel VBA
Continue learning classic Excel VBA with the same shared lesson template and cleaner visual style.
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.
7.1 Understanding Trigonometric Functions
Excel VBA provides these key trigonometric functions:
- Sin - Returns the sine of an angle
- Cos - Returns the cosine of an angle
- Tan - Returns the tangent of an angle
- Atn - Returns the arctangent (inverse tangent) of a number
7.2 Angle Conversion in VBA
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 π
7.3 The Sin Function
The Sin function returns the sine of an angle in radians.
Syntax:
Sin(angle_in_radians)
Example 7.1: Calculating Sine of 90 Degrees
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:
7.4 The Cos Function
The Cos function returns the cosine of an angle in radians.
Syntax:
Cos(angle_in_radians)
Example 7.2: Calculating Cosine of 60 Degrees
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:
7.5 The Tan Function
The Tan function returns the tangent of an angle in radians.
Syntax:
Tan(angle_in_radians)
Example 7.3: Calculating Tangent of 45 Degrees
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:
Summary
✅ In This Lesson, You Learned:
These trigonometric functions are fundamental for engineering, physics, and geometry applications in Excel VBA.
🔗 Related Resources