+ Reply to Thread
Results 1 to 9 of 9

VBA formula for distance always returns zero??

  1. #1
    Registered User
    Join Date
    04-13-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2003
    Posts
    26

    VBA formula for distance always returns zero??

    Hello:

    I’m trying to create a VBA function that will return the distance in statute miles between two points expressed in decimal latitude and longitude. So far, I have:

    Function Distxx(Lat1, Lon1, Lat2, Lon2)
    Distxx = RadiusEarth * ((2 * Application.WorksheetFunction.Asin(Sqr((Sin((Application.WorksheetFunction.Radians(Lat1) - Application.WorksheetFunction.Radians(Lat2)) / 2) ^ 2) + Cos(Application.WorksheetFunction.Radians(Lat1)) * Cos(Application.WorksheetFunction.Radians(Lat2)) * (Sin((Application.WorksheetFunction.Radians(Lon1) - Application.WorksheetFunction.Radians(Lon2)) / 2) ^ 2)))))
    End Function

    but the formula returns zero for all inputs that I have tried. Can anyone tell me what’s wrong with it?

    (And if there is a simpler way to do this, I would love to know what it is.)

    Thanks !!

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,990

    Re: VBA formula for distance always returns zero??

    How/where is RadiusEarth defined?
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    04-13-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: VBA formula for distance always returns zero??

    RadiusEarth in Statute Miles is a defined variable of: 4555.635783 miles

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: VBA formula for distance always returns zero??

    romper was asking where it is defined in your code, if it is not in the procedure, it will have a value of 0, the function will therefore always return 0

    Put Option Explicit right at the top of the module before all the other code. If it errors, the variable has not been defined
    Last edited by Kyle123; 06-14-2012 at 10:47 AM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA formula for distance always returns zero??

    If you use a function that returns central angle intead, you can mutiple by the earth's radius in whatever units you like to get distance:

    Please Login or Register  to view this content.
    E.g., =3959*CentralAngle(lat1, lon1, lat2, lon2) returns statute miles
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    04-13-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: VBA formula for distance always returns zero??

    Thanks for your responses. As is obvious, I suppose, I'm not very familiar with VBA; I'm just trying to get something that works.
    I"m not sure what Option Explicit does, so I just defined RadiusEarth within the Function. I would think that should work, but the result is still zero.
    I haven't tried the Central Angle formula but will work on it this afternoon.
    Here's the function with RadiusEarth defined (I think):
    Any other comments appreciated....

    Function Distxx(Lat1, Lon1, Lat2, Lon2)
    RadiusEarth = 4555.635783
    Distxx = RadiusEarth * ((2 * Application.WorksheetFunction.Asin(Sqr((Sin((Application.WorksheetFunction.Radians(Lat1) - Application.WorksheetFunction.Radians(Lat2)) / 2) ^ 2) + Cos(Application.WorksheetFunction.Radians(Lat1)) * Cos(Application.WorksheetFunction.Radians(Lat2)) * (Sin((Application.WorksheetFunction.Radians(Lon1) - Application.WorksheetFunction.Radians(Lon2)) / 2) ^ 2)))))
    End Function

  7. #7
    Registered User
    Join Date
    04-13-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: VBA formula for distance always returns zero??

    Just tried the CentralAngle formula and it worked pretty well. So thanks !! I will use that.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: VBA formula for distance always returns zero??

    Like this:

    Please Login or Register  to view this content.
    Last edited by Kyle123; 06-14-2012 at 12:30 PM.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: VBA formula for distance always returns zero??

    I don't know what planet has a radius of 4555 miles -- it's not Earth.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1