VBA Convert Degrees to Radians?
- Degrees
- 180 °
- You entered
This article explains how to convert degrees to radians in VBA (Visual Basic for Applications), the macro language used in Microsoft Office applications like Excel, Access, and Word. You will learn the simple formula, how to implement it as a custom function, and how to use VBA’s built-in WorksheetFunction.Radians for a one-line solution.
Formula
The mathematical relationship between degrees and radians is:
radians = degrees × (π / 180)
Where:
- radians – the angle in radians (dimensionless, often written as “rad”)
- degrees – the angle in degrees (°)
- π (pi) – approximately 3.14159265358979
This formula comes from the fact that a full circle is 360° and also 2π radians. Dividing both sides by 360 gives 1° = π/180 radians.
Worked Example
Using the default value of 180 degrees:
- Start with the formula: radians = degrees × (π / 180)
- Plug in 180 for degrees: radians = 180 × (3.14159265358979 / 180)
- Simplify: radians = 180 × 0.0174532925199433
- Result: radians = 3.14159265358979
So 180° equals π radians, which is exactly 3.14159265358979. The calculator above gives the same result to 6 decimal places: 3.141593.
How to Convert Degrees to Radians in VBA
There are two main ways to perform this conversion in VBA: using the built-in WorksheetFunction.Radians method, or writing your own function. Both are shown below.
Method 1: Using WorksheetFunction.Radians
If you are working in Excel VBA, you can call the same function that Excel uses:
Dim deg As Double
Dim rad As Double
deg = 45
rad = Application.WorksheetFunction.Radians(deg)
MsgBox rad ' Shows 0.785398163397448
This works in Excel, but not in other Office applications like Word or Access because they do not have a WorksheetFunction object.
Method 2: Custom VBA Function
For a portable solution that works in any VBA host, create a custom function:
Function DegToRad(deg As Double) As Double
DegToRad = deg * (Application.WorksheetFunction.Pi / 180)
End Function
Or, if you want to avoid the WorksheetFunction dependency entirely, use the constant:
Function DegToRad(deg As Double) As Double
Const pi As Double = 3.14159265358979
DegToRad = deg * (pi / 180)
End Function
You can then call this function from a macro or directly in an Excel worksheet cell like =DegToRad(90).
Common Mistakes and Pitfalls
- Forgetting to convert before using trig functions. VBA’s
Sin,Cos, andTanexpect radians, not degrees. If you pass degrees, you will get a wrong result. - Using integer division accidentally. If you write
deg * (pi / 180)withdegdeclared as an integer, VBA will round the result. Always declare variables asDouble. - Confusing π with 180. The conversion factor is π/180, not 180/π. The latter converts radians to degrees.
- Using a truncated π. Using 3.14 instead of 3.14159265358979 introduces a small error that can accumulate in long calculations.
When to Use This Conversion
You need to convert degrees to radians whenever you call a trigonometric function in VBA. For example:
- Calculating the sine, cosine, or tangent of an angle given in degrees.
- Computing distances or angles in geometric formulas (e.g., law of cosines).
- Working with polar coordinates or complex numbers.
- Simulating rotations or oscillations in engineering or physics macros.
If you are writing a macro that asks the user for an angle in degrees, you must convert it to radians before using it in Sin, Cos, Tan, or their inverses.
Alternatives to Manual Conversion
Besides the two VBA methods above, you can also:
- Use Excel’s built-in
RADIANSworksheet function directly in a cell:=RADIANS(180)returns 3.14159265358979. - Use the
Atnfunction to get π:4 * Atn(1)gives the most accurate π in VBA. - Use the
WorksheetFunction.Piproperty in Excel VBA for a high-precision π.
For the inverse conversion (radians to degrees), use the formula degrees = radians × (180 / π) or the WorksheetFunction.Degrees method.
Frequently Asked Questions
Does VBA have a built-in Radians function?
Yes, Excel’s VBA has Application.WorksheetFunction.Radians, which works exactly like the worksheet function. However, it is not available in other Office applications. For a universal solution, write your own function as shown above.
Why does VBA’s Sin function give the wrong answer for 90 degrees?
Because Sin expects radians, not degrees. Sin(90) treats 90 as radians, which is about 5156 degrees, and returns a value that is not 1. You must convert 90 degrees to radians first: Sin(90 * (π/180)) gives 1.
How do I convert radians to degrees in VBA?
Use the formula degrees = radians * (180 / π). For example, to convert 1.5708 radians to degrees: 1.5708 * (180 / 3.14159265358979) gives approximately 90. You can also use WorksheetFunction.Degrees in Excel.
What is the most accurate way to get π in VBA?
The most accurate way is to use 4 * Atn(1), because the Atn function returns the arctangent of 1, which is π/4. This avoids any hard-coded approximation.
Can I use the conversion in a worksheet cell without VBA?
Yes, Excel has a built-in RADIANS function. For example, =RADIANS(180) returns 3.14159265358979. You do not need VBA for simple conversions in a cell.
Related
Formula used
in deg | Degrees | ° | 180 | -100000 out rad | Radians | rad | deg * (3.14159265358979 / 180) | 6
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.