如何在同一行中添加多个VBA工作表函数

huangapple go评论59阅读模式
英文:

How do add multiple VBA WorksheetFunctions in the same line

问题

运行VBA代码时遇到了一些问题,用于计算最接近目的地的发射点的长公式。

运行时错误 438

对象不支持此属性或方法。

我已经尝试了几种不同的方法,但由于我对这方面还不熟悉,希望有人可以帮忙。

以下是代码:

Sub GetClosestLocation(dest_lat, dest_long)
Dim sp As Variant
Dim expr, a, b, c, launch_name, launch_lat, launch_long As Range

'Get Names on Launch Points sheet
a = Sheets("Launch Points").Range("F" & Rows.Count).End(xlUp).Row
expr = "F10" + Trim(Str(a))

Set launch_name = Sheets("Launch Points").Range(expr)

'Get Lattitudes on Launch Points sheet
b = Sheets("Launch Points").Range("B" & Rows.Count).End(xlUp).Row
expr = "B10" + Trim(Str(b))

Set launch_lat = Sheets("Launch Points").Range(expr)

'Get Longitudes on Launch Points sheet
c = Sheets("Launch Points").Range("C" & Rows.Count).End(xlUp).Row
expr = "C10" + Trim(Str(c))

Set launch_long = Sheets("Launch Points").Range(expr)

With Application
    sp = .Lookup(1, 1 / .Frequency(0, .Sin((.Radians(launch_lat - dest_lat)) / 2) ^ 2 + .Sin((.Radians(launch_long - dest_long)) / 2) ^ 2 * .Cos(.Radians(launch_lat)) * .Cos(.Radians(dest_lat))), launch_name)
End With

Range("F17") = sp

End Sub

我希望能够获取距离目的地最近的发射点。

在VBA之外运行代码没有问题。

英文:

Running into some issues with VBA and long formulas to calculate which launch point is closest to destination.

Run-Time Error 438

Object doesn't support this propert or method.

I have tried a few different ways of doing this and since I am still new to this I was hoping someone could help out.

Code below:

Sub GetClosestLocation(dest_lat, dest_long)
Dim sp As Variant
Dim expr, a, b, c, launch_name, launch_lat, launch_long As Range

'Get Names on Launch Points sheet
a = Sheets("Launch Points").Range("F" & Rows.Count).End(xlUp).Row
expr = "F10" + Trim(Str(a))

Set launch_name = Sheets("Launch Points").Range(expr)

'Get Lattitudes on Launch Points sheet
b = Sheets("Launch Points").Range("B" & Rows.Count).End(xlUp).Row
expr = "B10" + Trim(Str(b))

Set launch_lat = Sheets("Launch Points").Range(expr)

'Get Longitudes on Launch Points sheet
c = Sheets("Launch Points").Range("C" & Rows.Count).End(xlUp).Row
expr = "C10" + Trim(Str(c))

Set launch_long = Sheets("Launch Points").Range(expr)

With Application
    sp = .Lookup(1, 1 / .Frequency(0, .Sin((.Radians(launch_lat - dest_lat)) / 2) ^ 2 + .Sin((.Radians(launch_long - dest_long)) / 2) ^ 2 * .Cos(.Radians(launch_lat)) * .Cos(.Radians(dest_lat))), launch_name)
End With

    Range("F17") = sp
    
End Sub

I am hoping to get the launch point that would be closest to the destination.

Running the code outside of VBA works fine.

答案1

得分: 1

以下是代码部分的翻译:

Function GetClosestLocation(dest_lat, dest_long)
    
    Dim f As String, lr As Long, ws As Worksheet
    
    f = "=LOOKUP(1, 1 / FREQUENCY(0, SIN((RADIANS(B10:B119 - <dlat>)) / 2) ^ 2 + " & _
        "SIN((RADIANS(C10:C<lr> - <dlong>)) / 2) ^ 2 * COS(RADIANS(B10:B<lr>)) * " & _
        "COS(RADIANS(<dlat>))), F10:F<lr>)"
    
    Set ws = ThisWorkbook.Worksheets("Launch Points")
    
    lr = ws.Range("F" & Rows.Count).End(xlUp).Row
    f = Replace(f, "<lr>", lr)
    f = Replace(f, "<dlat>", dest_lat)
    f = Replace(f, "<dlong>", dest_long)
    
    GetClosestLocation = ws.Evaluate(f)
    
End Function

注意:这个翻译中已经去掉了 HTML 实体编码,以便在代码中正确使用。

英文:

Untested, since I don't have any test data, but something like this should work:

Function GetClosestLocation(dest_lat, dest_long)
    
    Dim f As String, lr As Long, ws As Worksheet
    
    f = &quot;=LOOKUP(1, 1 / FREQUENCY(0, SIN((RADIANS(B10:B119 - &lt;dlat&gt;)) / 2) ^ 2 + &quot; &amp; _
        &quot;SIN((RADIANS(C10:C&lt;lr&gt; - &lt;dlong&gt;)) / 2) ^ 2 * COS(RADIANS(B10:B&lt;lr&gt;)) * &quot; &amp; _
        &quot;COS(RADIANS(&lt;dlat&gt;))), F10:F&lt;lr&gt;)&quot;
    
    Set ws = ThisWorkbook.Worksheets(&quot;Launch Points&quot;)
    
    lr = ws.Range(&quot;F&quot; &amp; Rows.Count).End(xlUp).Row
    f = Replace(f, &quot;&lt;lr&gt;&quot;, lr)
    f = Replace(f, &quot;&lt;dlat&gt;&quot;, dest_lat)
    f = Replace(f, &quot;&lt;dlong&gt;&quot;, dest_long)
    
    GetClosestLocation = ws.Evaluate(f)
    
End Function

huangapple
  • 本文由 发表于 2023年4月4日 04:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定