problem with using Acos in VBA

blueflash

New Member
Joined
May 23, 2012
Messages
41
I am using excel 2016 on a mac(OS12.4)

I am trying to calculate the distance between points in lat long from data from an apple watch. I have downloaded the lat longs from the watch into a spreadsheet and have use the equation distxy=.... in the code below in a module and it ran a few times and gave the right answers (as far as I could tell).

I then copied the equation in another module and it ran several times without a problem but then stopped running. Now when I try to run the sub, I get a "Runtime error 1004. Method Acos of object WorksheetFunction failed" at he the equation. If I run the sub by stepping into it line by line, at the equation, I get an error"Runtime error 6. Overflow".

Can someone explain why I get two different errors and what I can do do fix the problem.
Thanks


Sub testgps()
Dim wf As WorksheetFunction
Dim lat1, lat2, long1, long2 As Double
Dim distxy As Double
Dim Radius As Single
Set wf = Application.WorksheetFunction

Radius = 6371
Worksheets("data").Activate
lastrow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 3 To lastrow
lat1 = Cells(i, "b").Value
lat2 = Cells(i - 1, "b").Value
long1 = Cells(i, "c").Value
long2 = Cells(i - 1, "c").Value
distxy = wf.Acos(Cos(wf.Radians(90 - lat1)) * Cos(wf.Radians(90 - lat2)) _
+ Sin(wf.Radians(90 - lat1)) * Sin(wf.Radians(90 - lat2)) _
* Cos(wf.Radians(long1 - long2))) * Radius * 1000
Cells(i, "j").Value = distxy
Next i
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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