To convert degrees to radians for use in trigonometric functions in VBA, the easiest way is with the Excel worksheet function Radians().
radians = WorksheetFunction.Radians(60)
The VBA collection of math functions has many powerful functions which make performing certain calculations in VBA very easy.
Unfortunately, VBA doesn’t have a function which converts degrees to radians, but we can use the Excel worksheet function Radians() to convert degrees to radians easily.
We can convert degrees to radians easily with the Excel worksheet function Radians().
To do so, we need to pass any number to the Radians() function.
Below are a few examples of how to use the Radians() function to convert different angles, in degrees, to radians with in VBA.
Debug.Print WorksheetFunction.Radians(0)
Debug.Print WorksheetFunction.Radians(30)
Debug.Print WorksheetFunction.Radians(60)
Debug.Print WorksheetFunction.Radians(90)
'Output:
0
0.523598775598299
1.0471975511966
1.5707963267949
If you’d like to go the other way, converting radians to degrees, you can use the Excel Degrees() worksheet function.
Converting Degrees to Radians Without Radians() Worksheet Function in VBA
Converting degrees to radians is a very easy formula. To convert degrees to radians, all we need to do is multiply the degrees by pi divided by 180.
We can convert degrees to radians without the help of the math module easily in VBA.
Below is a user-defined function which will convert degrees to radians for us in our VBA code.
Function degrees_to_radians(degrees As Double) As Double
degrees_to_radians = degrees * (WorksheetFunction.Pi / 180)
End Function
Let’s test the function to verify that we get the same results as the Radians() function.
Function degrees_to_radians(degrees As Double) As Double
degrees_to_radians = degrees * (WorksheetFunction.Pi / 180)
End Function
Debug.Print degrees_to_radians(0);
Debug.Print degrees_to_radians(30);
Debug.Print degrees_to_radians(60);
Debug.Print degrees_to_radians(90);
'Output:
0
0.523598775598299
1.0471975511966
1.5707963267949
As you can compare for yourself to the example above, we get the same results as using Radians().
Hopefully this post was helpful for you to learn how to convert degrees to radians in VBA with and without the Radians() Excel worksheet function.