Whole circle bearing calculation with vba

Westbury

Board Regular
Joined
Jun 7, 2009
Messages
139
Hi,

I'm trying to use this expression in a macro to obtain the whole circle bearing between 2 points of known latitude and longitude, the LatA Lonb etc (all in radians)


[FONT=&quot]360-(MOD(ATAN2((COS(LatA)*SIN(LatB))-(SIN(LatA)*COS(LatB)*COS(LonB-LonA)), SIN(LonB-LonA)*COS(LatB)),2*PI())*180/PI())


I think ATAN2 has to be called up as a worksheetfunction, but how do I use MOD in a macro?

Thanks
Geoff [/FONT]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Assuming LatA, LatB, LonA, LonB are vba variables you could use the Evaluate function like this:

Code:
Sub EvalIt()
Dim LatA, LatB, LonA, LonB, WholeCirBearing
LatA = 30.267153: LonA = -97.743057: LatB = 29.760427: LonB = -95.369804
WholeCirBearing = Evaluate("360-(MOD(ATAN2((COS(" & LatA & ")*SIN(" & LatB & _
    "))-(SIN(" & LatA & ")*COS(" & LatB & ")*COS(" & LonB - LonA & ")), SIN(" & _
    LonB - LonA & ")*COS(" & LatB & ")),2*PI())*180/PI())")
MsgBox WholeCirBearing
End Sub

As a caution, I know nothing about whole circle bearings so I may be missing the boat here altogether, but the values I used for the variables are the Lat/Lon for A is Austin,Tx and B is Houston, Tx. The answer I get is 170.534524833916 which means absolutely nothing to me. :eek:
 
Upvote 0
Joe, Thanks for your reply.

The whole circle bearing from Austin to Houston is approx 103 degrees. I've used an online calculator https://www.movable-type.co.uk/scripts/latlong.html to obtain this figure. [the WCB is the angle measured with zero at north to the line between the 2 points under investigation.

Your code produces a result but it's not correct. I'm sure that the formula agrees with mine which is widely available on the net.

Gone away to think....

Geoff
 
Upvote 0
Joe, Thanks for your reply.

The whole circle bearing from Austin to Houston is approx 103 degrees. I've used an online calculator https://www.movable-type.co.uk/scripts/latlong.html to obtain this figure. [the WCB is the angle measured with zero at north to the line between the 2 points under investigation.

Your code produces a result but it's not correct. I'm sure that the formula agrees with mine which is widely available on the net.

Gone away to think....

Geoff
Can't help you with the answer produced,nor the validity of the equation you posted for whole circle bearing, but the method of calculation using VBA is what I wanted to convey.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top