'粗体部分是调试器突出显示的部分 - 我不明白为什么它不起作用。我尝试在函数语句中使用lat_origin
,long_origin
,dest_lat
和dest_long
作为变体,但之后它说我在使用“Dim”语句时会重复。如果我没有在函数中包含dim语句,那么它表示我的变量没有定义,并突出显示“Radians”,这是一个标准的excel函数。
Option Explicit
Function LATLONDISTANCE(lat_origin As Single, long_origin As Single, dest_lat As Single, dest_long As Single) As Variant
Worksheets("Combined").Activate
ActiveSheet.Cells(2, 10).Select
Dim lat_origin As Variant
Dim long_origin As Variant
Dim dest_lat As Variant
Dim dest_long As Variant
'I don't want to include these "Dim statements, but if I don't, then "Radians" in the LATLONDISTANCE formula is highlighted, saying that it's not defined
LATLONDISTANCE = 0.621371 * (6371.1 * ((2 * Asin(SQRT((Sin((Radians(lat_origin) - Radians(dest_lat)) / 2) ^ 2) + Cos(Radians(lat_origin)) * Cos(Radians(dest_lat)) * (Sin((Radians(long_origin) - Radians(dest_long)) / 2) ^ 2))))))
End Function
Private Sub CommandButton1_Click()
Dim latlong(185, 1) As Variant
Dim iCounter1 As Integer, iCounter2 As Integer, iCounter3 As Integer, iCounter4 As Integer
Dim distance(185, 185) As Variant
Dim lat_origin As Variant
Dim long_origin As Variant
Dim dest_lat As Variant
Dim dest_long As Variant
Worksheets("Combined").Activate
Range("I2").Activate
For iCounter1 = 0 To 1
For iCounter2 = 0 To 185
latlong(iCounter2, iCounter1) = ActiveCell.Offset(iCounter2, iCounter1).Value
Next iCounter2
Next iCounter1
For iCounter1 = 0 To 1
For iCounter2 = 0 To 185
latlong(iCounter2, iCounter1) = ActiveCell.Offset(iCounter2, iCounter1).Value
Next iCounter2
Next iCounter1
For iCounter4 = 0 To 185 Step 1
lat_origin = latlong(iCounter4, 0)
long_origin = latlong(iCounter4, 1)
For iCounter3 = 0 To 185 Step 1
dest_lat = latlong(iCounter3, 0)
dest_long = latlong(iCounter3, 1)
distance(iCounter4, iCounter3) = LATLONDISTANCE(lat_origin, long_origin, dest_lat, dest_long)
Range(Cells(2, 14), Cells(187, 201)) = distance(iCounter4, iCounter3)
Next iCounter3
Next iCounter4
For iCounter4 = 0 To 185 Step 1
lat_origin = latlong(iCounter4, 0)
long_origin = latlong(iCounter4, 1)
For iCounter3 = 0 To 185 Step 1
dest_lat = latlong(iCounter3, 0)
dest_long = latlong(iCounter3, 1)
distance(iCounter4, iCounter3) = LATLONDISTANCE(**lat_origin**, long_origin, dest_lat, dest_long)
Range(Cells(2, 14), Cells(187, 201)) = distance(iCounter4, iCounter3)
Next iCounter3
Next iCounter4
End Sub
答案 0 :(得分:2)
您将所有论据声明为Single
s
Function LATLONDISTANCE(lat_origin As Single, long_origin As Single, dest_lat As Single, dest_long As Single) As Variant
但是你试着传递Variant
。你不能这样做。您还必须将变量声明为Single
。
Dim lat_origin As Single
Dim long_origin As Single
Dim dest_lat As Single
Dim dest_long As Single