# Excel VBA Cos Function for Radians and Degrees (3 Examples)

The **Cos **function returns the cosine of an angle, which is a ratio of the adjacent side of a right-angled triangle to the hypotenuse. Along with the basic Excel **COS **function, Excel has a dedicated **Cos **function in VBA.

**Purpose of the VBA Cos Function**

The VBA **Cos **function returns the cosine values for a radian angle.

**Syntax of the VBA Cos Function**

`Cos (Number As Double)`

**Arguments of the VBA Cos Function**

Argument | Explanation |

Number | The Number argument is the radian angle value for which the cosine value is calculated |

**Example #1: Get Cosine Values for Radian Angles**

The** VBA Cos function** can be applied to a single cell. To calculate the cosine values for a range, you need the following VBA code.

- Copy the codes and paste them into your required Module.

```
Sub Cos_Radian()
Dim xCell As Range
'Apply For Loop in a Range with Cos Function
For Each xCell In Range("B4:B9")
xCell.Offset(0, 1) = Cos(xCell.Value)
Next xCell
End Sub
```

- See the output result for applying the Cos function in a range.

**Example #2: Get Cosine Values for Degree Angles with VBA Pi Formula**

The **VBA Cos function **does not perform for degree angles. You need to modify the codes. To create a formula to convert degrees to radians and vice versa, we will use the **VBA Pi** function. The degree values will first be converted into radians, and then the Cos function will be used. The radian angles will be converted to degrees after receiving the Cosine values. Thus, we will finally see the Cosine values for the degrees.

- Copy the codes and paste them into your required Module.

```
Sub Cos_Radian()
Dim xCell As Range
'Apply ForSub Cos_Degree()
Dim xDegree As Range
Dim rad_to_deg As Range
Dim xCell As Range
Set xDegree = Range("B4:B9")
'Apply For Loop in a Range to convert degrees into radian
For Each xDegree In Range("B4:B9")
xDegree.Offset(0, 0) = (xDegree * Application.Pi) / 180
Next xDegree
'Apply For Loop in a Range with Cos Function
For Each xCell In Range("B4:B9")
xCell.Offset(0, 1) = Cos(xCell.Value)
Next xCell
Set rad_to_deg = Range("B4:B9")
'Apply For Loop in a Range to convert degrees into radian
For Each rad_to_deg In Range("B4:B9")
rad_to_deg.Offset(0, 0) = (rad_to_deg * 180) / Application.Pi
Next rad_to_deg
End Sub Loop in a Range with Cos Function
For Each xCell In Range("B4:B9")
xCell.Offset(0, 1) = Cos(xCell.Value)
Next xCell
End Sub
```

- See the image below, the cosine values are calculated for the degree values.

**Example #3: Get Cosine Values for Degree Angles with VBA WorkSheetFunction**

Other than using the VBA Pi function, we can use the **WorkSheetFunction.Radians** and **WorkSheetFunction.Degrees **part to transform radians and degrees with each other.

- Copy the codes and paste them into your required Module.

```
Sub Cos_WorkSheet_Degree()
Dim xDegree As Range
Dim rad_to_deg As Range
Dim xCell As Range
Set xDegree = Range("B4:B9")
'Apply For Loop in a Range to convert degrees into radian
For Each xDegree In Range("B4:B9")
xDegree.Offset(0, 0) = Application.WorksheetFunction.Radians(xDegree.Value)
Next xDegree
'Apply For Loop in a Range with Cos Function
For Each xCell In Range("B4:B9")
xCell.Offset(0, 1) = Cos(xCell.Value)
Next xCell
Set rad_to_deg = Range("B4:B9")
'Apply For Loop in a Range to convert degrees into radian
For Each rad_to_deg In Range("B4:B9")
rad_to_deg.Offset(0, 0) = Application.WorksheetFunction.Degrees(rad_to_deg.Value)
Next rad_to_deg
End Sub
```

- See the final output with cosine values for angles in degrees.

**Things to Keep in Mind**

- The angle must be stated in radians rather than degrees. Use the
**Radians**function to convert degrees to radians. - The range of the input value must be between
**-3.14159265358979**and**3.14159265358979**, which is approximately**-180**to**180**degrees. - the
**Cos**function returns values only within the range of -1 to 1. If the input value is outside the valid range, the function returns a**#NUM!**Error. - the
**Cos**function can only be used with numeric values. If you pass a non-numeric value to the function, it will return a**#VALUE!**error.

**Frequently Asked Questions**

**1. In VBA, how do I convert degrees to radians?**

- The
**Application.WorksheetFunction.Radians**method can be used to convert the result into radians.

**2. In VBA, what is the inverse function of Cos?**

- The inverse function of
**Cos**is**Acos**, which returns the angle whose cosine is a given value.

**3. Can complex numbers be used with the Cos function?**

- No, the
**Cos**function only works with real numbers. If you need to work with complex numbers, you will need to use a different function or library.

**Conclusion**

This VBA Cos function allows users to quickly calculate the cosine of a given angle, which is useful in a number of fields such as physics, engineering, and finance. These, in our opinion, will improve your professional work life somehow. If you have any additional questions, kindly post them in the comment section below. You will answer by offering solutions from the **Solved Excel** research department. Stay safe and get connected with us!

Hairstyles VIPI am curious to find out what blog system you’re working with? I’m having some minor security issues with my latest website and I’d like to find something more risk-free. Do you have any suggestions?

Fitnessnaturally like your website however you need to test the spelling on quite a few of your posts. Many of them are rife with spelling problems and I find it very bothersome to tell the truth however IæŠ£l definitely come again again.