递归飞行路径代码VBA语法错误

时间:2015-03-26 23:25:39

标签: excel-vba recursion syntax-error flightpath vba

当我运行此代码时,我收到语法错误。 我无法理解原因。
我粘贴了所有代码。如果你需要,我可以添加数据。
我想写一些recurvise代码,以便在时间限制下找到所有可能的飞行死记硬背。
find_route函数应该自行运行直到满足停止条件。(flight_time> 480)

Function find_route(a As Integer, b As Integer) 'a start node, b start time
flight_time = 0
route(0) = a
l = 0
If flight_time <= 480 Then

    If temp_flight_time(a, b) + flight_time <= 480 Then
    l = l + 1
    route(l) = next_destination(a, b)
    flight_time = temp_flight_time(a, b) + flight_time

         find_route(route(l),flight_time)'*******syntax error at this row******

    End If


Else
    Cells(7, 1).Select
    For i = 0 To 30
        ActiveCell.Value = route(i)
        ActiveCell.Offset(0, 1).Select
    Next
    Cells(1, 1).Select
    Exit Function

End If

End Function

Function temp_flight_time(a As Integer, b As Integer)
temp_flight_time = get_flight_time(a, next_destination(a, b))
End Function
Function get_flight_time(a As Integer, b As Integer) 'a from, b to
Cells(2, 1).Select
Dim ae As Integer
Dim be As Integer
For i = 1 To 50
    ae = ActiveCell.Value   'From
    be = ActiveCell.Offset(0, 1).Value  'To
    If a = ae And b = be Then
        get_flight_time = ActiveCell.Offset(0, 4).Value 'Flight Time
        Exit For
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Next
End Function
Function next_destination(a As Integer, b As Integer)
Cells(2, 1).Select
Dim ae As Integer
Dim be As Integer
For i = 1 To 50
    ae = ActiveCell.Value  'To
    be = ActiveCell.Offset(0, 2).Value  'Departure
    If a = ae And b <= be Then
        next_destination = ActiveCell.Offset(0, 1).Value
        Exit For
    Else
        ActiveCell.Offset(1, 0).Select
    End If
Next
End Function

1 个答案:

答案 0 :(得分:0)

第一行:Function find_route(a As Integer, b As Integer)

find_route是一个函数,因此它返回一个值。未指定返回类型,因此返回Variant。

问题专栏:find_route(route(l),flight_time)

调用该函数但是没有地方放置返回值。这会导致语法错误,因为您必须对VBA中的返回值执行某些操作。

由于您从未设置函数的返回值(您可以通过编写find_route = ...来完成),因此您可能希望将其设置为“Sub”。 Subs不会返回任何内容,因此该行现在可以正常。

只需将第一行更改为Sub find_route...,将相应的“结束函数”更改为“结束子”