# My first custom function--the haversine formula

#### Forums

Hi

my first post and a chance to say how useful i have found this site and the linked videos

I have recently been tasked with testing how far customers laddresses are from the business. I found a data base which had the longitude and latitude of every UK postcode,  armed with that information i came up with this custom function

Option ExplicitFunction getDistance(latitude1 As Double,longitude1 As Double,latitude2 As Double,longitude2 As Double) As DoubleDim earth_radius As DoubleDim Pi As DoubleDim deg2rad As Double

Dim dLat As DoubleDim dLon As DoubleDim a As DoubleDim c As DoubleDim d As Double

dLat = deg2rad * (latitude2 - latitude1)dLon = deg2rad * (longitude2 - longitude1)

a = Sin(dLat / 2) * Sin(dLat / 2) + Cos(deg2rad * latitude1) * Cos(deg2rad * latitude2) * Sin(dLon / 2) * Sin(dLon / 2)c = 2 * Sqr(Atn(a / Sqr(1 - (a*a))))

getdistance = d

End Function

I couldn't have done with out the help of the Youtube videos on custom functioms and a little help with the  trigonometery  from Google

### Hey Michael, that is great to

Brian Element Wed, 12/01/2021 - 09:59

Hey Michael, that is great to hear.  Thanks for sharing the code.  I hate to break it to you but there was one that already did this in passport which I have attached.  You can have a look and hopefully it gives the same results as yours.

Thanks for letting me know.  It gives me incentives to keep creating videos and adding information to the site.

### well mine is in kilometers...

Michael Davey Wed, 12/01/2021 - 10:25

well mine is in kilometers...
Its more satisfying to write your own code particulary when its my first time in IDEA. I have learned few things looking at the passport version as well so its all good.

keep up the good work!

### Yes it is.  I find it fun to

Brian Element Wed, 12/01/2021 - 10:31

Yes it is.  I find it fun to write code.  Kind of like putting a puzzle together.  Keep writing those functions.