Lesson 7

Excel VBA Lesson 7: Trigonometric Functions in Excel VBA

Continue learning classic Excel VBA with the same shared lesson template and cleaner visual style.

Classic Excel VBA Shared modern template Ad-free lesson layout

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

Important: VBA trigonometric functions use radians rather than degrees. You must convert between these units.

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:

VBA Sin function example output showing result of 1
Figure 7.1: Output showing Sin(90°) = 1

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:

VBA Cos function example output showing result of 0.5
Figure 7.2: Output showing Cos(60°) = 0.5

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:

VBA Tan function example output showing result of 1
Figure 7.3: Output showing Tan(45°) = 1

Summary

✅ In This Lesson, You Learned:

  • VBA trigonometric functions use radians, not degrees
  • Convert degrees to radians using: radians = degrees × (π/180)
  • Get π precisely with: pi = 4 * Atn(1)
  • Sin(), Cos(), and Tan() are essential for mathematical calculations
  • Use Round() to control decimal places in results

These trigonometric functions are fundamental for engineering, physics, and geometry applications in Excel VBA.

🔗 Related Resources