Excel VBA Cos Function
VBA Cos Function: The Cos function in VBA returns the cosine value for a supplied angle.
Syntax
Cos (Number)
Parameter
Number (required) –This parameter represents the number that you want the absolute value of.
Return
This function returns the cosine value for a supplied angle.
Example 1
Sub CosFunction_Example1() ' Calculating the cosine for the specified number. Dim cos_val1 As Double Dim cos_val2 As Double Dim cos_val3 As Double cos_val1 = Cos(90) ' The variable cos_val1 will return value -0.448073616. Cells(1, 1).Value = cos_val1 cos_val2 = Cos(0) ' The variable cos_val2 will return value 1. Cells(2, 1).Value = cos_val2 cos_val3 = Cos(3.14159265358979) ' The variable cos_val3 will return value -1. Cells(3, 1).Value = cos_val3 End Sub
Output
-0.448073616 |
1 |
-1 |
Example 2
Sub CosFunction_Example2() ' Calculating the cosine for the specified number. Dim cos_val As Double cos_val1 = Cos(0.785398163397448) ' The variable cos_val1 will return value 0. Cells(1, 1).Value = cos_val End Sub
Output
0
Example 3
Sub CosFunction_Example3() ' Calculating the cosine for the specified number. Dim cos_val As Double Const pi = 3.14159265358979 ' Converting 60 degrees to radians by multiplying by pi/180. cos_val = Cos(60 * pi / 180) ' The variable cos_val will return radina 0.500000000000001. Cells(1, 1).Value = cos_val End Sub
Output
Example 4
Sub CosFunction_Example4() ' Calculating the cosine for the specified number. Dim cos_val As Double 'for other formats apart from number cos_val = Cos("42/a") ' The variable atn_val1 will return mismatch error. Cells(1, 1).Value = cos_val End Sub
Output