Multiple Apps

Radian to degree and degree to radian conversion functions

Ease of Use

Easy

Version tested with

2000 

Submitted by:

MWE

Description:

Rad2Deg converts from radians to degrees. Deg2Rad converts from degrees to radians. 

Discussion:

Many engineering and scientific applications perform computations requiring conversion between degrees and radians. The conversion is quite simple IF you can remember the conversion formula. Rad2Deg and Deg2Rad perform these conversions; and, not being humanoids, they tend to remember the conversion factors. These functions will work for any VB/VBA application. The demonstration is Excel based, although it shines when used in other applications, since Excel already has native RADIANS and DEGREES functions. 

Code:

instructions for use

			

Option Explicit Function Deg2Rad(Deg) As Double ' ' Function converts degrees to radians ' Passed Values: ' Deg [in, numeric] value in degrees to be converted ' Deg2Rad = Deg / 57.2957795130823 End Function Function Rad2Deg(Rad) As Double ' ' Function converts radians to degrees ' Passed Values: ' Rad [in, numeric] value in radians to be converted ' Rad2Deg = 57.2957795130823 * Rad End Function Sub DegRad_Test() ' ' Demonstration: interacts with use to demonstrate trig-related conversion functions ' Dim Ans As String Dim I As Integer Dim Title As String Dim X As Single Title = "Test of degree < > radian conversion functions" GetInitial: Ans = InputBox("enter test #:" & vbCrLf & _ "1 convert from degrees to radians" & vbCrLf & _ "2 convert from radians to degrees" & vbCrLf & _ "[enter nothing or click on Cancel to quit]", Title) If Ans = "" Then Exit Sub I = Ans Select Case I Case Is = 1 GetCase1: Ans = InputBox("value in degrees ?", "Demo of Deg2Rad") If Ans = "" Then GoTo GetInitial X = Ans MsgBox X & " degrees = " & Format(Deg2Rad(X), "##0.###") & " radians", _ vbInformation, Title GoTo GetCase1 Case Is = 2 GetCase2: Ans = InputBox("value in radians ?", "Demo of Rad2Deg") If Ans = "" Then GoTo GetInitial X = Ans MsgBox X & " radians = " & Format(Rad2Deg(X), "##0.###") & " degrees", _ vbInformation, Title GoTo GetCase2 End Select GoTo GetInitial End Sub

How to use:

  1. Copy the above code.
  2. Open any workbook.
  3. Press Alt + F11 to open the Visual Basic Editor (VBE).
  4. In the left side window, hi-lite the target spreadsheet [it will likely be called VBAProject(filename.xls) where filename is the name of the spreadsheet]
  5. Select an existing code module for the target worksheet; or from the Insert Menu, choose Insert | Module.
  6. Paste the code into the right-hand code window.
  7. Close the VBE, save the file if desired.
  8. See ?Test The Code? below
 

Test the code:

  1. Open the example
  2. The example contains a testing procedure, and the conversion functions. The test procedure prompts for what the user wants to test, then prompts for test values and finally displays results. After each cycle, the procedure cycles back and reprompts for new input. Entering nothing or clicking on Cancel ?backs up? the test procedure one level.
  3. NOTE: these procedures will ultimately be called by some parent procedure or application. Thus final testing will depend on how the procs are to be used.
 

Sample File:

Rad2Deg.zip 21.29KB 

Approved by mdmackillop


This entry has been viewed 63 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express