VBA COS Function

HomeVBA Functions LIST (Category Wise)How to use the VBA COS Function (Syntax + Example)

What is VBA COS Function

The VBA COS function is listed under the math category of VBA functions. When you use it in a VBA code, it returns the cosine of a supplied angle. In simple words, a cosine of an angle is the length of the adjacent side of a right triangle, divided by the length of the hypotenuse which this function returns.

how to use it

To use VBA's COS function you need to understand its syntax and arguments:

Syntax

Cos(Number)

Arguments

  • Number: The angle value for which you want to calculate the cosine.

Example to use COS Function in VBA

To practically understand how to use VBA COS function, you need to go through the below example where we have written a vba code by using it:

Sub example_COS()
Range("B1").Value = Cos(Range("A1"))
End Sub

In the above example, we have used the value from cell A1 (12) and then we have used the COS function to get the cosine of a supplied angle and it has returned 0.843853958732492 in the result.

Notes

Below are some important points which you need to take care while using COS function in VBA.

  • If the value specified is a value other than a number or a string which can’t be recognized as a number, VBA will return the run-time 13 error.
  • You can multiply the result returned by the COS with pi/180 to convert into a radians and 180/pi to convert it into degrees.

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.