I am writing macro function to calculate distance between two GPS coordinates. I am new to writing macros and Basic and not entirely sure what I’m doing.

Libre Calc 7.3.5.2 (x64)

Win 10 21H2

However, after much ponderous hacking, the macro function appears to work. The following is the contents of Module in My Macros and Dialogs.

```
REM Option Explicit
Function GeoDistance(lat1 as Double,lon1 as Double,lat2 as Double,lon2 as Double)
Dim Lat1Radians as Double
Dim Lat2Radians as Double
Dim DistLat as Double
Dim DistLon as Double
Dim oService as Object
Dim varA as Double
Set oService = createUnoService("com.sun.star.sheet.FunctionAccess")
Lat1Radians = oService.callFunction( "Radians", Array(lat1))
Lat2Radians = oService.callFunction( "Radians", Array(lat2))
Lon1Radians = oService.callFunction( "Radians", Array(lon1))
Lon2Radians = oService.callFunction( "Radians", Array(lon2))
DistLat = Lat1Radians-Lat2Radians
DistLon = Lon1Radians-Lon2Radians
varA = sin(DistLat/2)*sin(DistLat/2)+sin(DistLon/2)*sin(DistLon/2)*cos(Lat1Radians)*cos(Lat2Radians)
varB = 2*oService.callFunction( "Atan2", Array(sqr(1-varA),sqr(varA)))
earthRadius = 6371
distance = earthRadius*varB*1000
GeoDistance = distance
'GeoDistance = 999
End Function
```

If I call function `GeoDistance(args...)`

from Calc sheet it appears to work (although very slow - which is maybe for another topic).

However, if I click the `+`

sign adjacent to Sheet at bottom taskbar, Calc begins creating endless new sheets. I have to click top-right `X`

to close app without saving.

What is wrong?