To convert radians to degrees for use in trigonometric functions in VBA, the easiest way is with the Excel worksheet function Degrees().
degrees = WorksheetFunction.Degrees(WorksheetFunction.Pi/2)
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 radians to degrees, but we can use the Excel worksheet function Degrees() to convert radians to degrees easily.
We can convert radians to degrees easily with the Excel worksheet function Degrees().
To do so, we need to pass any number to the Degrees() worksheet function.
Below are a few examples of how to use the Degrees() worksheet function to convert different angles, in radians, to degrees with in VBA.
Debug.Print WorksheetFunction.Degrees(0)
Debug.Print WorksheetFunction.Degrees(WorksheetFunction.Pi/6)
Debug.Print WorksheetFunction.Degrees(WorksheetFunction.Pi/3)
Debug.Print WorksheetFunction.Degrees(WorksheetFunction.Pi/2)
'Output:
0
30
60
90
If you’d like to go the other way, converting degrees to radians, you can use the Radians() Excel worksheet function.
Converting Radians to Degrees Without the Degrees() Worksheet function in VBA
Converting radians to degrees is a very easy formula. To convert radians to degrees, all we need to do is multiply the degrees by 180 and divide by pi.
We can convert radians to degrees without the help of the math module easily in VBA.
Below is a user-defined function which will convert radians to degrees for us in our VBA code.
Function radians_to_degrees(radians as Double) as Double
radians_to_degrees = radians * (180 / WorksheetFunction.Pi)
End Function
Let’s test the function to verify that we get the same results as the Degrees() function.
Function radians_to_degrees(radians as Double) as Double
radians_to_degrees = radians * (180 / WorksheetFunction.Pi)
End Function
Debug.Print radians_to_degrees(0);
Debug.Print radians_to_degrees(WorksheetFunction.Pi/6);
Debug.Print radians_to_degrees(WorksheetFunction.Pi/3);
Debug.Print radians_to_degrees(WorksheetFunction.Pi/2);
'Output:
0
30
60
90
As you can compare for yourself to the example above, we get the same results as using Degrees().
Hopefully this post was helpful for you to learn how to convert radians to degrees in VBA with and without the Degrees() Excel worksheet function.