To find the cosine of a number (in radians) with VBA, we can use the VBA Cos() function.
In VBA, we can easily use trigonometric functions from the collection of VBA math functions. These VBA math functions allow us to perform trigonometry easily.
To find the cosine of a number, in radians, we use the VBA Cos() function.
Below is the VBA syntax to find the cosine of a number.
The input to the Cos() function must be a double. The return value will be a double between -1 and 1.
Debug.Print Cos(WorksheetFunction.Pi/3) Debug.Print Cos(0) Debug.Print Cos(WorksheetFunction.Pi/2) 'Output: 0.5000000000000001 1.0 6.123233995736766e-17
Converting Degrees to Radians for Input into Cos()
The Cos() function takes a number in radians as input. If your data is in degrees, you will need to convert the values to radians.
To convert degrees to radians, we can use the Worksheet Function Radians() to convert the value to radians and then pass it to the Cos() function.
Debug.Print Cos(WorksheetFunction.Radians(60)) Debug.Print Cos(WorksheetFunction.Radians(0)) Debug.Print Cos(WorksheetFunction.Radians(90)) 'Output: 0.5000000000000001 1.0 6.123233995736766e-17
Finding the Inverse Cosine of a Number in VBA
With VBA, we can also find the inverses of the common trigonometric functions. The Acos() Worksheet function allows us to find the inverse of the cosine of a number.
Below, we show that if we pass a number to Cos() and then call the VBA Acos() worksheet function, we get back the same number.
Debug.Print WorksheetFunction.Acos(Cos(WorksheetFunction.Pi/3)) Debug.Print WorksheetFunction.Pi/3 'Output: 1.0471975511965976 1.0471975511965976
Finding the Secant of a Number in VBA
To find the secant of a number, we can divide 1 by the cosine of the number.
We can find the secant of a number easily with the VBA Cos() function. You can see how to find the secant of a number in the following VBA code.
Debug.Print 1/Cos(WorksheetFunction.Pi/3) 'Output: 1.9999999999999996
Hopefully this article has been beneficial for you to understand how to use the Cos() function in VBA to find the cosine of a number.