atan2

  • Thread starter Thread starter Ken
  • Start date Start date
K

Ken

Anyone know the way to code an ATAN2 function using the
functions provided by MS? Thanks, Ken
 
Hi Ken

Here's the Atan2 function. Simply copy/paste this into a new module

Best Regards

Maurice St-Cyr
Micro Systems Consultants, Inc.



Function ATan2(x As Double, Y As Double) As Double
'
' Returns the ArcTangent based on X and Y coordinates
' If both X and Y are zero an error will occur.
'
' The positive X axis is assumed to be 0, going poistive in the
' counterclockwise direction, and negative in the clockwise direction.
'
If x = 0 Then
If Y = 0 Then
ATan2 = 1 / 0
ElseIf Y > 0 Then
ATan2 = PI() / 2
Else
ATan2 = -PI() / 2
End If
ElseIf x > 0 Then
If Y = 0 Then
ATan2 = 0
Else
ATan2 = Atn(Y / x)
End If
Else
If Y = 0 Then
ATan2 = PI()
Else
ATan2 = (PI() - Atn(Abs(Y) / Abs(x))) * Sgn(Y)
End If
End If
End Function
 
I haven't seen the PI() function in VBA / Access VBA.

Is it an inbuilt function or a UDF?

If you are using the Excel PI() function, then you need to automate Excel
and in this case, it is perhaps more convenient to simply use the Atan2()
function in Excel.
 
I haven't seen the PI() function in VBA / Access VBA.

Is it an inbuilt function or a UDF?

If you are using the Excel PI() function, then you need to automate Excel
and in this case, it is perhaps more convenient to simply use the Atan2()
function in Excel.

In order to avoid Excel automation, you also could store Pi as a
constant:

Const Pi As Double = 3.14159265358979

or compute it

Dim Pi As Double
Pi = 4 * Atn(1)

Greetings
Matthias Kläy
 
Back
Top